aboutsummaryrefslogtreecommitdiff
path: root/mysql/mysys_ssl/my_aes_yassl.cpp
blob: f387e34ba86327761222ec431e3a12a50d8e2434 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* Copyright (c) 2015, 2016 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_aes_impl.h"

#include "aes.hpp"
#include "openssl/ssl.h"

/* keep in sync with enum my_aes_opmode in my_aes.h */
const char *my_aes_opmode_names[]=
{
  "aes-128-ecb",
  "aes-192-ecb",
  "aes-256-ecb",
  "aes-128-cbc",
  "aes-192-cbc",
  "aes-256-cbc",
  NULL /* needed for the type enumeration */
};


/* keep in sync with enum my_aes_opmode in my_aes.h */
static uint my_aes_opmode_key_sizes_impl[]=
{
  128 /* aes-128-ecb */,
  192 /* aes-192-ecb */,
  256 /* aes-256-ecb */,
  128 /* aes-128-cbc */,
  192 /* aes-192-cbc */,
  256 /* aes-256-cbc */,
};

uint *my_aes_opmode_key_sizes= my_aes_opmode_key_sizes_impl;


template <TaoCrypt::CipherDir DIR>
class MyCipherCtx
{
public:
  MyCipherCtx(enum my_aes_opmode mode) : m_mode(mode)
  {
    switch (m_mode)
    {
    case my_aes_128_ecb:
    case my_aes_192_ecb:
    case my_aes_256_ecb:
      m_need_iv= false;
      break;
    default:
      m_need_iv= true;
      break;
    }
  }

  bool SetKey(const unsigned char *key, uint block_size,
              const unsigned char *iv)
  {
    if (m_need_iv)
    {
      if (!iv)
        return TRUE;
      cbc.SetKey(key, block_size, iv);
    }
    else
      ecb.SetKey(key, block_size);
    return false;
  }

  void Process(unsigned char *dest, const unsigned char * source,
               uint block_size)
  {
    if (m_need_iv)
      cbc.Process(dest, source, block_size);
    else
      ecb.Process(dest, source, block_size);
  }

  bool needs_iv() const
  {
    return m_need_iv;
  }

private:
  /* we initialize the two classes to avoid dynamic allocation */
  TaoCrypt::BlockCipher<DIR, TaoCrypt::AES, TaoCrypt::ECB> ecb;
  TaoCrypt::BlockCipher<DIR, TaoCrypt::AES, TaoCrypt::CBC> cbc;
  enum my_aes_opmode m_mode;
  bool m_need_iv;
};


int my_aes_encrypt(const unsigned char *source, uint32 source_length,
                   unsigned char *dest,
                   const unsigned char *key, uint32 key_length,
                   enum my_aes_opmode mode, const unsigned char *iv,
                   bool padding)
{
  MyCipherCtx<TaoCrypt::ENCRYPTION> enc(mode);

  /* 128 bit block used for padding */
  unsigned char block[MY_AES_BLOCK_SIZE];
  uint num_blocks;                               /* number of complete blocks */
  uint i;
  /* predicted real key size */
  const uint key_size= my_aes_opmode_key_sizes[mode] / 8;
  /* The real key to be used for encryption */
  unsigned char rkey[MAX_AES_KEY_LENGTH / 8];

  my_aes_create_key(key, key_length, rkey, mode);

  if (enc.SetKey(rkey, key_size, iv))
    return MY_AES_BAD_DATA;

  num_blocks= source_length / MY_AES_BLOCK_SIZE;

  /* Encode all complete blocks */
  for (i = num_blocks; i > 0;
       i--, source+= MY_AES_BLOCK_SIZE, dest+= MY_AES_BLOCK_SIZE)
       enc.Process(dest, source, MY_AES_BLOCK_SIZE);

  /* If no padding, return here */
  if (!padding)
	  return (int) (MY_AES_BLOCK_SIZE * num_blocks);
  /*
  Re-implement standard PKCS padding for the last block.
  Pad the last incomplete data block (even if empty) with bytes
  equal to the size of extra padding stored into that last packet.
  This also means that there will always be one more block,
  even if the source data size is dividable by the AES block size.
  */
  unsigned char pad_len=
    MY_AES_BLOCK_SIZE - (source_length - MY_AES_BLOCK_SIZE * num_blocks);
  memcpy(block, source, MY_AES_BLOCK_SIZE - pad_len);
  memset(block + MY_AES_BLOCK_SIZE - pad_len, pad_len, pad_len);

  enc.Process(dest, block, MY_AES_BLOCK_SIZE);

  /* we've added a block */
  num_blocks+= 1;

  return (int) (MY_AES_BLOCK_SIZE * num_blocks);
}

int my_aes_decrypt(const unsigned char *source, uint32 source_length,
                   unsigned char *dest,
                   const unsigned char *key, uint32 key_length,
                   enum my_aes_opmode mode, const unsigned char *iv,
                   bool padding)
{
  MyCipherCtx<TaoCrypt::DECRYPTION> dec(mode);
  /* 128 bit block used for padding */
  uint8 block[MY_AES_BLOCK_SIZE];
  uint32 num_blocks;                               /* Number of complete blocks */
  int i;
  /* predicted real key size */
  const uint key_size= my_aes_opmode_key_sizes[mode] / 8;
  /* The real key to be used for decryption */
  unsigned char rkey[MAX_AES_KEY_LENGTH / 8];

  my_aes_create_key(key, key_length, rkey, mode);
  dec.SetKey(rkey, key_size, iv);

  num_blocks= source_length / MY_AES_BLOCK_SIZE;

  /*
  Input size has to be a multiple of the AES block size.
  And, due to the standard PKCS padding, at least one block long.
  */
  if ((source_length != num_blocks * MY_AES_BLOCK_SIZE) || num_blocks == 0)
    return MY_AES_BAD_DATA;

  /* Decode all but the last block */
  for (i= padding? num_blocks - 1: num_blocks; i > 0;
       i--, source+= MY_AES_BLOCK_SIZE, dest+= MY_AES_BLOCK_SIZE)
       dec.Process(dest, source, MY_AES_BLOCK_SIZE);

  /* If no padding, return here. */
  if (!padding)
	  return MY_AES_BLOCK_SIZE * num_blocks;

  /* unwarp the standard PKCS padding */
  dec.Process(block, source, MY_AES_BLOCK_SIZE);

  /* Use last char in the block as size */
  uint8 pad_len = block[MY_AES_BLOCK_SIZE - 1];

  if (pad_len > MY_AES_BLOCK_SIZE)
    return MY_AES_BAD_DATA;
  /* We could also check whole padding but we do not really need this */

  memcpy(dest, block, MY_AES_BLOCK_SIZE - pad_len);
  return MY_AES_BLOCK_SIZE * num_blocks - pad_len;
}

/**
 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
 @param mode           encryption mode

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

int my_aes_get_size(uint32 source_length, my_aes_opmode opmode)
{
  return MY_AES_BLOCK_SIZE * (source_length / MY_AES_BLOCK_SIZE)
    + MY_AES_BLOCK_SIZE;
}

/**
  Return true if the AES cipher and block mode requires an IV

  SYNOPSIS
  my_aes_needs_iv()
  @param mode           encryption mode

  @retval TRUE   IV needed
  @retval FALSE  IV not needed
*/

my_bool my_aes_needs_iv(my_aes_opmode opmode)
{
  MyCipherCtx<TaoCrypt::ENCRYPTION> enc(opmode);

  return enc.needs_iv() ? TRUE : FALSE;
}