summaryrefslogtreecommitdiff
path: root/mysys_ssl/my_aes.cc
blob: 20bd03551c25cd871ea942539397eecd2e688091 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
/* 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>

#if defined(HAVE_YASSL)
#include "aes.hpp"
#include "openssl/ssl.h"
#include "crypto_wrapper.hpp"
#elif defined(HAVE_OPENSSL)
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>
#include <openssl/conf.h>

// Wrap C struct, to ensure resources are released.
struct MyCipherCtx
{
  MyCipherCtx() { memset(&ctx, 0, sizeof(ctx)); }
  ~MyCipherCtx() { EVP_CIPHER_CTX_cleanup(&ctx); }

  EVP_CIPHER_CTX ctx;
};
#endif

enum encrypt_dir { MY_AES_ENCRYPT, MY_AES_DECRYPT };

/**
  This is internal function just keeps joint code of Key generation

  SYNOPSIS
    my_aes_create_key()
    @param key        [in]       Key to use for real key creation
    @param key_length [in]       Length of the key
    @param rkey       [out]      Real key (used by OpenSSL/YaSSL)

  @return
    0         Ok
    -1        Error; Note: The current impementation never returns this
*/

static int my_aes_create_key(const char *key, int key_length, uint8 *rkey)
{
  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*/

  memset(rkey, 0, AES_KEY_LENGTH / 8);          /* Set initial key  */

  for (ptr= rkey, sptr= key; sptr < key_end; ptr ++, sptr ++)
  {
    if (ptr == rkey_end)
      /*  Just loop over tmp_key until we used all key */
      ptr= rkey;
    *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
  return 0;
}

/**
   Decode Hexencoded String to uint8[].

   SYNOPSIS
   my_aes_hex2uint()
   @param iv        [in]	Pointer to hexadecimal encoded IV String
   @param dest      [out]	Pointer to output uint8 array. Memory allocated by caller
   @param iv_length [in]  Size of destination array.
 */

void my_aes_hex2uint(const char* in, unsigned char *out, int dest_length)
{
  const char *pos= in;
  int count;
  for (count = 0; count < dest_length; count++)
  {
    uchar res;
    sscanf(pos, "%2hhx", &res);
    out[count] = res;
    pos += 2 * sizeof(char);
  }
}


/**
   Calculate key and iv from a given salt and secret as it is handled
   in openssl encrypted files via console

   SYNOPSIS
   my_bytes_to_key()
   @param salt   [in]  the given salt as extracted from the encrypted file
   @param secret [in]  the given secret as String, provided by the user
   @param key    [out] 32 Bytes of key are written to this pointer
   @param iv     [out] 16 Bytes of iv are written to this pointer
*/

void my_bytes_to_key(const unsigned char *salt, const char *secret, unsigned char *key,
                     unsigned char *iv)
{
#ifdef HAVE_YASSL
  /* the yassl function has no support for SHA1. Reason unknown. */
  int keyLen = 32;
  int ivLen  = 16;
  int EVP_SALT_SZ = 8;
  const int SHA_LEN = 20;
  yaSSL::SHA myMD;
  uint digestSz = myMD.get_digestSize();
  unsigned char digest[SHA_LEN];                   // max size
  int sz = strlen(secret);
  int count = 1;
  int keyLeft   = keyLen;
  int ivLeft    = ivLen;
  int keyOutput = 0;

  while (keyOutput < (keyLen + ivLen))
  {
    int digestLeft = digestSz;
    if (keyOutput)                      // first time D_0 is empty
      myMD.update(digest, digestSz);
    myMD.update((yaSSL::byte* )secret, sz);
    if (salt)
      myMD.update(salt, EVP_SALT_SZ);
    myMD.get_digest(digest);
    for (int j = 1; j < count; j++)
    {
      myMD.update(digest, digestSz);
      myMD.get_digest(digest);
    }

    if (keyLeft)
    {
      int store = MY_MIN(keyLeft, static_cast<int>(digestSz));
      memcpy(&key[keyLen - keyLeft], digest, store);

      keyOutput  += store;
      keyLeft    -= store;
      digestLeft -= store;
    }

    if (ivLeft && digestLeft)
    {
      int store = MY_MIN(ivLeft, digestLeft);
      memcpy(&iv[ivLen - ivLeft], &digest[digestSz - digestLeft], store);

      keyOutput += store;
      ivLeft    -= store;
    }
  }
#elif defined(HAVE_OPENSSL)
  const EVP_CIPHER *type = EVP_aes_256_cbc();
  const EVP_MD *digest = EVP_sha1();
  EVP_BytesToKey(type, digest, salt, (uchar*) secret, strlen(secret), 1, key, iv);
#endif
}

/**
  Crypt buffer with AES CBC encryption algorithm.

  SYNOPSIS
     my_aes_encrypt_cbc()
     @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]  if set to true, no padding is used. if the input length is not a
      	  	  	  	  	  	  	 multiple of the AES block size, trailing bytes are only copied to destination buffer.
      	  	  	  	  	  	  	 This allows currently the same interface for CBC, ECB and CTR encryption.
  @return
    != 0           error
    0             no error
*/

static int my_aes_encrypt_cbc(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)
{
  uint8 remaining_bytes = (noPadding == 0) ? 0 : source_length % MY_AES_BLOCK_SIZE;
  source_length = source_length - remaining_bytes;

#ifdef HAVE_YASSL
  TaoCrypt::AES_CBC_Encryption enc;
  /* 128 bit block used for padding */
  uint8 block[MY_AES_BLOCK_SIZE];
  int num_blocks;                               /* number of complete blocks */
  int i;
  switch(key_length) {
  case 16:
    break;
  case 24:
    break;
  case 32:
    break;
  default:
    return AES_BAD_KEYSIZE;
  }

  enc.SetKey((const TaoCrypt::byte *) key, key_length, (const TaoCrypt::byte *) iv);

  num_blocks = source_length / MY_AES_BLOCK_SIZE;

  for (i = num_blocks; i > 0; i--)              /* Encode complete blocks */
  {
    enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) source,
                MY_AES_BLOCK_SIZE);
    source += MY_AES_BLOCK_SIZE;
    dest += MY_AES_BLOCK_SIZE;
  }

  if (noPadding) {
	if (remaining_bytes!=0) {
	   memcpy(dest + source_length, source + source_length, remaining_bytes);
	}
    *dest_length = MY_AES_BLOCK_SIZE * (num_blocks) + remaining_bytes;
    return AES_OK;

  }

  /* Encode the rest. We always have incomplete block */
  char pad_len = MY_AES_BLOCK_SIZE - (source_length -
                                      MY_AES_BLOCK_SIZE * num_blocks);
  memcpy(block, source, 16 - pad_len);
  memset(block + MY_AES_BLOCK_SIZE - pad_len, pad_len,  pad_len);

  enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) block,
              MY_AES_BLOCK_SIZE);

  *dest_length = MY_AES_BLOCK_SIZE * (num_blocks + 1);
  return AES_OK;
#elif defined(HAVE_OPENSSL)
  MyCipherCtx ctx;
  int u_len, f_len;
  /* The real key to be used for encryption */
  const EVP_CIPHER* cipher;
  switch(key_length) {
  case 16:
    cipher = EVP_aes_128_cbc();
    break;
  case 24:
    cipher = EVP_aes_192_cbc();
    break;
  case 32:
    cipher = EVP_aes_256_cbc();
    break;
  default:
    return AES_BAD_KEYSIZE;
  }
  //Initialize Encryption Engine here, default software Engine is default
  ENGINE *engine = NULL;

  if (! EVP_EncryptInit_ex(&ctx.ctx, cipher, engine, key, iv))
    return AES_BAD_DATA;                        /* Error */
  if (noPadding) {
    EVP_CIPHER_CTX_set_padding(&ctx.ctx, 0);
  }
  EVP_CIPHER_CTX_key_length(&ctx.ctx);
  OPENSSL_assert(EVP_CIPHER_CTX_key_length(&ctx.ctx) == key_length);
  OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx.ctx) == iv_length);
  OPENSSL_assert(EVP_CIPHER_CTX_block_size(&ctx.ctx) == 16);
  if (! EVP_EncryptUpdate(&ctx.ctx, (unsigned char *) dest, &u_len,
                          (unsigned const char *) source, source_length))
    return AES_BAD_DATA;                        /* Error */
  if (! EVP_EncryptFinal_ex(&ctx.ctx, (unsigned char *) dest + u_len, &f_len))
    return AES_BAD_DATA;                        /* Error */

  if (remaining_bytes!=0) {
 	  memcpy(dest + source_length, source + source_length, remaining_bytes);
   }
  *dest_length = (unsigned long int) (u_len + f_len + remaining_bytes);

  return AES_OK;
#else
  /* currently Open SSL is required */
  return AES_BAD_DATA;
#endif
}


/**
  Crypt buffer with AES ECB encryption algorithm.

  SYNOPSIS
     my_aes_encrypt_ecb()
     @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]  if set to true, no padding is used. if the input length is not a
      	  	  	  	  	  	  	 multiple of the AES block size, trailing bytes are only copied to destination buffer.
      	  	  	  	  	  	  	 This allows currently the same interface for CBC, ECB and CTR encryption.
  @return
    != 0           error
    0             no error
*/

static int my_aes_encrypt_ecb(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)
{
  uint8 remaining_bytes = (noPadding == 0) ? 0 : source_length % MY_AES_BLOCK_SIZE;
  source_length = source_length - remaining_bytes;

#ifdef HAVE_YASSL
  TaoCrypt::AES_ECB_Encryption enc;
  /* 128 bit block used for padding */
  uint8 block[MY_AES_BLOCK_SIZE];
  int num_blocks;                               /* number of complete blocks */
  int i;
  switch(key_length) {
  case 16:
    break;
  case 24:
    break;
  case 32:
    break;
  default:
    return AES_BAD_KEYSIZE;
  }

  enc.SetKey((const TaoCrypt::byte *) key, key_length, (const TaoCrypt::byte *) iv);

  num_blocks = source_length / MY_AES_BLOCK_SIZE;

  for (i = num_blocks; i > 0; i--)              /* Encode complete blocks */
  {
    enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) source,
                MY_AES_BLOCK_SIZE);
    source += MY_AES_BLOCK_SIZE;
    dest += MY_AES_BLOCK_SIZE;
  }

  if (noPadding) {
	if (remaining_bytes!=0) {
	   memcpy(dest + source_length, source + source_length, remaining_bytes);
	}
	*dest_length = MY_AES_BLOCK_SIZE * (num_blocks) + remaining_bytes;
    return AES_OK;

  }

  /* Encode the rest. We always have incomplete block */
  char pad_len = MY_AES_BLOCK_SIZE - (source_length -
                                      MY_AES_BLOCK_SIZE * num_blocks);
  memcpy(block, source, 16 - pad_len);
  memset(block + MY_AES_BLOCK_SIZE - pad_len, pad_len,  pad_len);

  enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) block,
              MY_AES_BLOCK_SIZE);

  *dest_length = MY_AES_BLOCK_SIZE * (num_blocks + 1);
  return AES_OK;
#elif defined(HAVE_OPENSSL)
  MyCipherCtx ctx;
  int u_len, f_len;
  /* The real key to be used for encryption */
  const EVP_CIPHER* cipher;
  switch(key_length) {
  case 16:
    cipher = EVP_aes_128_ecb();
    break;
  case 24:
    cipher = EVP_aes_192_ecb();
    break;
  case 32:
    cipher = EVP_aes_256_ecb();
    break;
  default:
    return AES_BAD_KEYSIZE;
  }
  //Initialize Encryption Engine here, default software Engine is default
  ENGINE *engine = NULL;

  if (! EVP_EncryptInit_ex(&ctx.ctx, cipher, engine, key, iv))
    return AES_BAD_DATA;                        /* Error */
  if (noPadding) {
    EVP_CIPHER_CTX_set_padding(&ctx.ctx, 0);
  }
  EVP_CIPHER_CTX_key_length(&ctx.ctx);
  OPENSSL_assert(EVP_CIPHER_CTX_key_length(&ctx.ctx) == key_length);
  OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx.ctx) == iv_length);
  OPENSSL_assert(EVP_CIPHER_CTX_block_size(&ctx.ctx) == 16);
  if (! EVP_EncryptUpdate(&ctx.ctx, (unsigned char *) dest, &u_len,
                          (unsigned const char *) source, source_length))
    return AES_BAD_DATA;                        /* Error */
  if (! EVP_EncryptFinal_ex(&ctx.ctx, (unsigned char *) dest + u_len, &f_len))
    return AES_BAD_DATA;                        /* Error */

  if (remaining_bytes!=0) {
   	  memcpy(dest + source_length, source + source_length, remaining_bytes);
     }
  *dest_length = (unsigned long int) (u_len + f_len + remaining_bytes);

  return AES_OK;
#else
  /* currently Open SSL is required */
  return AES_BAD_DATA;
#endif
}



/**
  AES decryption - CBC mode

  SYNOPSIS
     my_aes_decrypt_cbc()
     @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]  if set to true, no padding is used. if the input length is not a
      	  	  	  	  	  	  	 multiple of the AES block size, trailing bytes are only copied to destination buffer.
      	  	  	  	  	  	  	 This allows currently the same interface for CBC, ECB and CTR encryption.

  @return
    != 0           error
    0             no error
*/

static int my_aes_decrypt_cbc(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)
{
	uint8 remaining_bytes = (noPadding == 0) ? 0 : source_length % MY_AES_BLOCK_SIZE;
	source_length = source_length - remaining_bytes;


#ifdef HAVE_YASSL
  TaoCrypt::AES_CBC_Decryption dec;
  /* 128 bit block used for padding */
  uint8 block[MY_AES_BLOCK_SIZE];
  uint num_blocks;                               /* Number of complete blocks */
  int i;
  switch(key_length) {
  case 16:
    break;
  case 24:
    break;
  case 32:
    break;
  default:
    return AES_BAD_KEYSIZE;
  }

  dec.SetKey((const TaoCrypt::byte *) key, key_length, iv);

  num_blocks = source_length / MY_AES_BLOCK_SIZE;

  if ((source_length != num_blocks * MY_AES_BLOCK_SIZE) || num_blocks == 0 )
    /* Input size has to be even and at least one block */
    return AES_BAD_DATA;

  /* Decode all but last blocks */
  for (i = num_blocks - 1; i > 0; i--)
  {
    dec.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) source,
                MY_AES_BLOCK_SIZE);
    source += MY_AES_BLOCK_SIZE;
    dest += MY_AES_BLOCK_SIZE;
  }

  dec.Process((TaoCrypt::byte *) block, (const TaoCrypt::byte *) source,
              MY_AES_BLOCK_SIZE);

  if (noPadding) {
    memcpy(dest, block, MY_AES_BLOCK_SIZE);
    if (remaining_bytes!=0) {
            	  memcpy(dest + source_length, source + source_length, remaining_bytes);
    }
    *dest_length = MY_AES_BLOCK_SIZE * num_blocks + remaining_bytes;
    return AES_OK;
  }

  /* Use last char in the block as size */
  uint pad_len = (uint) (uchar) block[MY_AES_BLOCK_SIZE - 1];

  if (pad_len > MY_AES_BLOCK_SIZE)
    return 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);
  *dest_length = MY_AES_BLOCK_SIZE * num_blocks - pad_len;
  return  AES_OK;
#elif defined(HAVE_OPENSSL)
  MyCipherCtx ctx;
  int u_len, f_len;

  const EVP_CIPHER* cipher;
  switch(key_length) {
  case 16:
    cipher = EVP_aes_128_cbc();
    break;
  case 24:
    cipher = EVP_aes_192_cbc();
    break;
  case 32:
    cipher = EVP_aes_256_cbc();
    break;
  default:
    return AES_BAD_KEYSIZE;
  }
  //Initialize Encryption Engine here, default software Engine is default
  ENGINE *engine = NULL;

  if (! EVP_DecryptInit_ex(&ctx.ctx, cipher, engine, key, iv))
    return AES_BAD_DATA;                        /* Error */
  if (noPadding) {
    EVP_CIPHER_CTX_set_padding(&ctx.ctx, 0);
  }
  OPENSSL_assert(EVP_CIPHER_CTX_key_length(&ctx.ctx) == key_length);
  OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx.ctx) == iv_length);
  OPENSSL_assert(EVP_CIPHER_CTX_block_size(&ctx.ctx) == 16);
  if (! EVP_DecryptUpdate(&ctx.ctx, (unsigned char *) dest, &u_len,
                          (unsigned char *)source, source_length))
    return AES_BAD_DATA;                        /* Error */
  if (! EVP_DecryptFinal_ex(&ctx.ctx, (unsigned char *) dest + u_len, &f_len)) {
    *dest_length = (unsigned long int) u_len;
    return AES_BAD_DATA;
  }
  if (remaining_bytes!=0) {
      	  memcpy(dest + source_length, source + source_length, remaining_bytes);
  }
  *dest_length = (unsigned long int) (u_len + f_len) + remaining_bytes;
#endif
  return AES_OK;
}

/**
  AES decryption - ECB mode

  SYNOPSIS
     my_aes_decrypt_ecb()
     @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]  if set to true, no padding is used. if the input length is not a
      	  	  	  	  	  	  	 multiple of the AES block size, trailing bytes are only copied to destination buffer.
      	  	  	  	  	  	  	 This allows currently the same interface for CBC, ECB and CTR encryption.

  @return
    != 0           error
    0             no error
*/

static int my_aes_decrypt_ecb(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)
{
  uint8 remaining_bytes = (noPadding == 0) ? 0 : source_length % MY_AES_BLOCK_SIZE;
  source_length = source_length - remaining_bytes;


#ifdef HAVE_YASSL
  TaoCrypt::AES_ECB_Decryption dec;
  /* 128 bit block used for padding */
  uint8 block[MY_AES_BLOCK_SIZE];
  uint num_blocks;                               /* Number of complete blocks */
  int i;
  switch(key_length) {
  case 16:
    break;
  case 24:
    break;
  case 32:
    break;
  default:
    return AES_BAD_KEYSIZE;
  }

  dec.SetKey((const TaoCrypt::byte *) key, key_length, iv);

  num_blocks = source_length / MY_AES_BLOCK_SIZE;

  if ((source_length != num_blocks * MY_AES_BLOCK_SIZE) || num_blocks == 0 )
    /* Input size has to be even and at least one block */
    return AES_BAD_DATA;

  /* Decode all but last blocks */
  for (i = num_blocks - 1; i > 0; i--)
  {
    dec.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) source,
                MY_AES_BLOCK_SIZE);
    source += MY_AES_BLOCK_SIZE;
    dest += MY_AES_BLOCK_SIZE;
  }

  dec.Process((TaoCrypt::byte *) block, (const TaoCrypt::byte *) source,
              MY_AES_BLOCK_SIZE);

  if (noPadding) {
    memcpy(dest, block, MY_AES_BLOCK_SIZE);
    if (remaining_bytes!=0) {
      memcpy(dest + source_length, source + source_length, remaining_bytes);
    }
    *dest_length = MY_AES_BLOCK_SIZE * num_blocks + remaining_bytes;
    return AES_OK;
  }

  /* Use last char in the block as size */
  uint pad_len = (uint) (uchar) block[MY_AES_BLOCK_SIZE - 1];

  if (pad_len > MY_AES_BLOCK_SIZE)
    return 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);
  *dest_length = MY_AES_BLOCK_SIZE * num_blocks - pad_len;
  return  AES_OK;
#elif defined(HAVE_OPENSSL)
  MyCipherCtx ctx;
  int u_len, f_len;

  const EVP_CIPHER* cipher;
  switch(key_length) {
  case 16:
    cipher = EVP_aes_128_ecb();
    break;
  case 24:
    cipher = EVP_aes_192_ecb();
    break;
  case 32:
    cipher = EVP_aes_256_ecb();
    break;
  default:
    return AES_BAD_KEYSIZE;
  }
  //Initialize Encryption Engine here, default software Engine is default
  ENGINE *engine = NULL;

  if (! EVP_DecryptInit_ex(&ctx.ctx, cipher, engine, key, iv))
    return AES_BAD_DATA;                        /* Error */
  if (noPadding) {
    EVP_CIPHER_CTX_set_padding(&ctx.ctx, 0);
  }
  OPENSSL_assert(EVP_CIPHER_CTX_key_length(&ctx.ctx) == key_length);
  OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx.ctx) == iv_length);
  OPENSSL_assert(EVP_CIPHER_CTX_block_size(&ctx.ctx) == 16);
  if (! EVP_DecryptUpdate(&ctx.ctx, (unsigned char *) dest, &u_len,
                          (unsigned char *)source, source_length))
    return AES_BAD_DATA;                        /* Error */
  if (! EVP_DecryptFinal_ex(&ctx.ctx, (unsigned char *) dest + u_len, &f_len)) {
    *dest_length = (unsigned long int) u_len;
    return AES_BAD_DATA;
  }
  if (remaining_bytes!=0) {
    memcpy(dest + source_length, source + source_length, remaining_bytes);
  }
  *dest_length = (unsigned long int) (u_len + f_len) + remaining_bytes;

#endif
  return AES_OK;
}





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


/****************************************************************
  Encryption function visible to MariaDB users
****************************************************************/

int my_aes_encrypt(const uchar* source, int source_length, uchar* dest,
                   const char* key, int key_length)
{
#if defined(HAVE_YASSL)
  TaoCrypt::AES_ECB_Encryption enc;

  /* 128 bit block used for padding */
  uint8 block[MY_AES_BLOCK_SIZE];
  int num_blocks;                               /* number of complete blocks */
  int i;
#elif defined(HAVE_OPENSSL)
  MyCipherCtx ctx;
  int u_len, f_len;
#endif

  /* The real key to be used for encryption */
  uint8 rkey[AES_KEY_LENGTH / 8];
  int rc;                                       /* result codes */

  if ((rc= my_aes_create_key(key, key_length, rkey)))
    return rc;

#if defined(HAVE_YASSL)
  enc.SetKey((const TaoCrypt::byte *) rkey, MY_AES_BLOCK_SIZE);

  num_blocks = source_length / MY_AES_BLOCK_SIZE;

  for (i = num_blocks; i > 0; i--)              /* Encode complete blocks */
  {
    enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) source,
                MY_AES_BLOCK_SIZE);
    source += MY_AES_BLOCK_SIZE;
    dest += MY_AES_BLOCK_SIZE;
  }

  /* Encode the rest. We always have incomplete block */
  char pad_len = MY_AES_BLOCK_SIZE - (source_length -
                                      MY_AES_BLOCK_SIZE * num_blocks);
  memcpy(block, source, 16 - pad_len);
  memset(block + MY_AES_BLOCK_SIZE - pad_len, pad_len,  pad_len);

  enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) block,
              MY_AES_BLOCK_SIZE);

  return MY_AES_BLOCK_SIZE * (num_blocks + 1);
#elif defined(HAVE_OPENSSL)
  if (! EVP_EncryptInit(&ctx.ctx, EVP_aes_128_ecb(),
                        (const unsigned char *) rkey, NULL))
    return AES_BAD_DATA;                        /* Error */
  if (! EVP_EncryptUpdate(&ctx.ctx, (unsigned char *) dest, &u_len,
                          (unsigned const char *) source, source_length))
    return AES_BAD_DATA;                        /* Error */
  if (! EVP_EncryptFinal(&ctx.ctx, (unsigned char *) dest + u_len, &f_len))
    return AES_BAD_DATA;                        /* Error */

  return u_len + f_len;
#endif
}


/**
  DeCrypt buffer with AES encryption algorithm.

  SYNOPSIS
    my_aes_decrypt()
    @param source        [in]   Pointer to data for decryption
    @param source_length [in]   Size of encrypted data
    @param dest          [out]  Buffer to place decrypted data (must
                                be large enough)
    @param key           [in]   Key to be used for decryption
    @param key_length    [in]   Length of the key. Will handle keys of any length

  @return
    >= 0             Size of encrypted data
    < 0              Error
*/

int my_aes_decrypt(const uchar *source, int source_length, uchar *dest,
                   const char *key, int key_length)
{
#if defined(HAVE_YASSL)
  TaoCrypt::AES_ECB_Decryption dec;
  /* 128 bit block used for padding */
  uint8 block[MY_AES_BLOCK_SIZE];
  int num_blocks;                               /* Number of complete blocks */
  int i;
#elif defined(HAVE_OPENSSL)
  MyCipherCtx ctx;
  int u_len, f_len;
#endif

  /* The real key to be used for decryption */
  uint8 rkey[AES_KEY_LENGTH / 8];
  int rc;                                       /* Result codes */

  if ((rc= my_aes_create_key(key, key_length, rkey)))
    return rc;

#if defined(HAVE_YASSL)
  dec.SetKey((const TaoCrypt::byte *) rkey, MY_AES_BLOCK_SIZE);

  num_blocks = source_length / MY_AES_BLOCK_SIZE;

  if ((source_length != num_blocks * MY_AES_BLOCK_SIZE) || num_blocks == 0 )
    /* Input size has to be even and at least one block */
    return AES_BAD_DATA;

  /* Decode all but last blocks */
  for (i = num_blocks - 1; i > 0; i--)
  {
    dec.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) source,
                MY_AES_BLOCK_SIZE);
    source += MY_AES_BLOCK_SIZE;
    dest += MY_AES_BLOCK_SIZE;
  }

  dec.Process((TaoCrypt::byte *) block, (const TaoCrypt::byte *) source,
              MY_AES_BLOCK_SIZE);

  /* Use last char in the block as size */
  uint pad_len = (uint) (uchar) block[MY_AES_BLOCK_SIZE - 1];

  if (pad_len > MY_AES_BLOCK_SIZE)
    return 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;
#elif defined(HAVE_OPENSSL)
  if (! EVP_DecryptInit(&ctx.ctx, EVP_aes_128_ecb(),
                        (const unsigned char *) rkey, NULL))
    return AES_BAD_DATA;                        /* Error */
  if (! EVP_DecryptUpdate(&ctx.ctx, (unsigned char *) dest, &u_len,
                          (unsigned const char *) source, source_length))
    return AES_BAD_DATA;                        /* Error */
  if (! EVP_DecryptFinal(&ctx.ctx, (unsigned char *) dest + u_len, &f_len))
    return AES_BAD_DATA;                        /* Error */
  return u_len + f_len;
#endif
}


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