summaryrefslogtreecommitdiff
path: root/mysys_ssl/my_crypt.cc
blob: 7ca65b253e60de85755f3705c3c94995689b207d (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 Copyright (c) 2014 Google Inc.
 Copyright (c) 2014, 2015 MariaDB Corporation

 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 <my_crypt.h>

#ifdef HAVE_YASSL
#include "aes.hpp"

typedef TaoCrypt::CipherDir Dir;
static const Dir CRYPT_ENCRYPT = TaoCrypt::ENCRYPTION;
static const Dir CRYPT_DECRYPT = TaoCrypt::DECRYPTION;

typedef TaoCrypt::Mode CipherMode;
static inline CipherMode aes_ecb(uint) { return TaoCrypt::ECB; }
static inline CipherMode aes_cbc(uint) { return TaoCrypt::CBC; }

typedef TaoCrypt::byte KeyByte;

#else
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/err.h>

typedef int Dir;
static const Dir CRYPT_ENCRYPT = 1;
static const Dir CRYPT_DECRYPT = 0;

typedef const EVP_CIPHER *CipherMode;

#define make_aes_dispatcher(mode)                               \
  static inline CipherMode aes_ ## mode(uint key_length)        \
  {                                                             \
    switch (key_length) {                                       \
    case 16: return EVP_aes_128_ ## mode();                     \
    case 24: return EVP_aes_192_ ## mode();                     \
    case 32: return EVP_aes_256_ ## mode();                     \
    default: return 0;                                          \
    }                                                           \
  }

make_aes_dispatcher(ecb)
make_aes_dispatcher(cbc)

typedef uchar KeyByte;

struct MyCTX : EVP_CIPHER_CTX {
  MyCTX()  { EVP_CIPHER_CTX_init(this); }
  ~MyCTX() { EVP_CIPHER_CTX_cleanup(this); ERR_remove_state(0); }
};
#endif

static int block_crypt(CipherMode cipher, Dir dir,
                       const uchar* source, uint source_length,
                       uchar* dest, uint* dest_length,
                       const KeyByte *key, uint key_length,
                       const KeyByte *iv, uint iv_length, int no_padding)
{
  int tail= source_length % MY_AES_BLOCK_SIZE;

  if (likely(source_length >= MY_AES_BLOCK_SIZE || !no_padding))
  {
#ifdef HAVE_YASSL
    TaoCrypt::AES ctx(dir, cipher);

    if (unlikely(key_length != 16 && key_length != 24 && key_length != 32))
      return MY_AES_BAD_KEYSIZE;

    ctx.SetKey(key, key_length);
    if (iv)
    {
      ctx.SetIV(iv);
      DBUG_ASSERT(TaoCrypt::AES::BLOCK_SIZE <= iv_length);
    }
    DBUG_ASSERT(TaoCrypt::AES::BLOCK_SIZE == MY_AES_BLOCK_SIZE);

    ctx.Process(dest, source, source_length - tail);
    *dest_length= source_length - tail;

    /* unlike OpenSSL, YaSSL doesn't support PKCS#7 padding */
    if (!no_padding)
    {
      if (dir == CRYPT_ENCRYPT)
      {
        uchar buf[MY_AES_BLOCK_SIZE];
        memcpy(buf, source + source_length - tail, tail);
        memset(buf + tail, MY_AES_BLOCK_SIZE - tail, MY_AES_BLOCK_SIZE - tail);
        ctx.Process(dest + *dest_length, buf, MY_AES_BLOCK_SIZE);
        *dest_length+= MY_AES_BLOCK_SIZE;
      }
      else
      {
        int n= dest[source_length - 1];
        if (tail || n == 0 || n > MY_AES_BLOCK_SIZE)
          return MY_AES_BAD_DATA;
        *dest_length-= n;
      }
    }

#else // HAVE_OPENSSL
    int fin;
    struct MyCTX ctx;

    if (unlikely(!cipher))
      return MY_AES_BAD_KEYSIZE;

    if (!EVP_CipherInit_ex(&ctx, cipher, NULL, key, iv, dir))
      return MY_AES_OPENSSL_ERROR;

    EVP_CIPHER_CTX_set_padding(&ctx, !no_padding);

    DBUG_ASSERT(EVP_CIPHER_CTX_key_length(&ctx) == (int)key_length);
    DBUG_ASSERT(EVP_CIPHER_CTX_iv_length(&ctx) <= (int)iv_length);
    DBUG_ASSERT(EVP_CIPHER_CTX_block_size(&ctx) == MY_AES_BLOCK_SIZE);

    /* use built-in OpenSSL padding, if possible */
    if (!EVP_CipherUpdate(&ctx, dest, (int*)dest_length,
                          source, source_length - (no_padding ? tail : 0)))
      return MY_AES_OPENSSL_ERROR;
    if (!EVP_CipherFinal_ex(&ctx, dest + *dest_length, &fin))
      return MY_AES_BAD_DATA;
    *dest_length += fin;

#endif
  }

  if (no_padding)
  {
    if (tail)
    {
      /*
        Not much we can do, block ciphers cannot encrypt data that aren't
        a multiple of the block length. At least not without padding.
        Let's do something CTR-like for the last partial block.
      */

      uchar mask[MY_AES_BLOCK_SIZE];
      uint mlen;

      DBUG_ASSERT(iv_length >= sizeof(mask));
      my_aes_encrypt_ecb(iv, sizeof(mask), mask, &mlen,
                         key, key_length, 0, 0, 1);
      DBUG_ASSERT(mlen == sizeof(mask));

      const uchar *s= source + source_length - tail;
      const uchar *e= source + source_length;
      uchar *d= dest + source_length - tail;
      const uchar *m= mask;
      while (s < e)
        *d++ = *s++ ^ *m++;
    }
    *dest_length= source_length;
  }

  return MY_AES_OK;
}

C_MODE_START

#ifdef HAVE_EncryptAes128Ctr
make_aes_dispatcher(ctr)

/*
  special simplified implementation for CTR, because it's a stream cipher
  (doesn't need padding, always encrypts the specified number of bytes), and
  because encrypting and decrypting code is exactly the same (courtesy of XOR)
*/
int my_aes_encrypt_ctr(const uchar* source, uint source_length,
                       uchar* dest, uint* dest_length,
                       const uchar* key, uint key_length,
                       const uchar* iv, uint iv_length)
{
  CipherMode cipher= aes_ctr(key_length);
  struct MyCTX ctx;
  int fin __attribute__((unused));

  if (unlikely(!cipher))
    return MY_AES_BAD_KEYSIZE;

  if (!EVP_CipherInit_ex(&ctx, cipher, NULL, key, iv, CRYPT_ENCRYPT))
    return MY_AES_OPENSSL_ERROR;

  DBUG_ASSERT(EVP_CIPHER_CTX_key_length(&ctx) == (int)key_length);
  DBUG_ASSERT(EVP_CIPHER_CTX_iv_length(&ctx) == (int)iv_length);
  DBUG_ASSERT(EVP_CIPHER_CTX_block_size(&ctx) == 1);

  if (!EVP_CipherUpdate(&ctx, dest, (int*)dest_length, source, source_length))
    return MY_AES_OPENSSL_ERROR;

  DBUG_ASSERT(EVP_CipherFinal_ex(&ctx, dest + *dest_length, &fin));
  DBUG_ASSERT(fin == 0);

  return MY_AES_OK;
}

#endif /* HAVE_EncryptAes128Ctr */

int my_aes_encrypt_ecb(const uchar* source, uint source_length,
                       uchar* dest, uint* dest_length,
                       const uchar* key, uint key_length,
                       const uchar* iv, uint iv_length,
                       int no_padding)
{
  return block_crypt(aes_ecb(key_length), CRYPT_ENCRYPT, source, source_length,
                     dest, dest_length, key, key_length, iv, iv_length, no_padding);
}

int my_aes_decrypt_ecb(const uchar* source, uint source_length,
                       uchar* dest, uint* dest_length,
                       const uchar* key, uint key_length,
                       const uchar* iv, uint iv_length,
                       int no_padding)
{
  return block_crypt(aes_ecb(key_length), CRYPT_DECRYPT, source, source_length,
                     dest, dest_length, key, key_length, iv, iv_length, no_padding);
}

int my_aes_encrypt_cbc(const uchar* source, uint source_length,
                       uchar* dest, uint* dest_length,
                       const uchar* key, uint key_length,
                       const uchar* iv, uint iv_length,
                       int no_padding)
{
  return block_crypt(aes_cbc(key_length), CRYPT_ENCRYPT, source, source_length,
                     dest, dest_length, key, key_length, iv, iv_length, no_padding);
}

int my_aes_decrypt_cbc(const uchar* source, uint source_length,
                       uchar* dest, uint* dest_length,
                       const uchar* key, uint key_length,
                       const uchar* iv, uint iv_length,
                       int no_padding)
{
  return block_crypt(aes_cbc(key_length), CRYPT_DECRYPT, source, source_length,
                     dest, dest_length, key, key_length, iv, iv_length, no_padding);
}

C_MODE_END

#if defined(HAVE_YASSL)

#include <random.hpp>

C_MODE_START

int my_random_bytes(uchar* buf, int num)
{
  TaoCrypt::RandomNumberGenerator rand;
  rand.GenerateBlock((TaoCrypt::byte*) buf, num);
  return MY_AES_OK;
}

C_MODE_END

#else  /* OpenSSL */

#include <openssl/rand.h>

C_MODE_START

int my_random_bytes(uchar* buf, int num)
{
  /*
    Unfortunately RAND_bytes manual page does not provide any guarantees
    in relation to blocking behavior. Here we explicitly use SSLeay random
    instead of whatever random engine is currently set in OpenSSL. That way
    we are guaranteed to have a non-blocking random.
  */
  RAND_METHOD* rand = RAND_SSLeay();
  if (rand == NULL || rand->bytes(buf, num) != 1)
    return MY_AES_OPENSSL_ERROR;
  return MY_AES_OK;
}

C_MODE_END
#endif /* HAVE_YASSL */

/**
  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;
}