1 #ifndef MY_AES_INCLUDED
2 #define MY_AES_INCLUDED
3 
4 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License, version 2.0,
8  as published by the Free Software Foundation.
9 
10  This program is also distributed with certain software (including
11  but not limited to OpenSSL) that is licensed under separate terms,
12  as designated in a particular file or component or in included license
13  documentation.  The authors of MySQL hereby grant you an additional
14  permission to link the program and your derivative works with the
15  separately licensed software that they have included with MySQL.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  GNU General Public License, version 2.0, for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
25 
26 
27 /* Header file for my_aes.c */
28 /* Wrapper to give simple interface for MySQL to AES standard encryption */
29 
30 C_MODE_START
31 
32 /** AES IV size is 16 bytes for all supported ciphers except ECB */
33 #define MY_AES_IV_SIZE 16
34 
35 /** AES block size is fixed to be 128 bits for CBC and ECB */
36 #define MY_AES_BLOCK_SIZE 16
37 
38 
39 /** Supported AES cipher/block mode combos */
40 enum my_aes_opmode
41 {
42    my_aes_128_ecb,
43    my_aes_192_ecb,
44    my_aes_256_ecb,
45    my_aes_128_cbc,
46    my_aes_192_cbc,
47    my_aes_256_cbc
48    ,my_aes_128_cfb1,
49    my_aes_192_cfb1,
50    my_aes_256_cfb1,
51    my_aes_128_cfb8,
52    my_aes_192_cfb8,
53    my_aes_256_cfb8,
54    my_aes_128_cfb128,
55    my_aes_192_cfb128,
56    my_aes_256_cfb128,
57    my_aes_128_ofb,
58    my_aes_192_ofb,
59    my_aes_256_ofb
60 };
61 
62 #define MY_AES_BEGIN my_aes_128_ecb
63 #define MY_AES_END my_aes_256_ofb
64 
65 /* If bad data discovered during decoding */
66 #define MY_AES_BAD_DATA  -1
67 
68 /** String representations of the supported AES modes. Keep in sync with my_aes_opmode */
69 extern const char *my_aes_opmode_names[];
70 
71 /**
72   Encrypt a buffer using AES
73 
74   @param source         [in]  Pointer to data for encryption
75   @param source_length  [in]  Size of encryption data
76   @param dest           [out] Buffer to place encrypted data (must be large enough)
77   @param key            [in]  Key to be used for encryption
78   @param key_length     [in]  Length of the key. Will handle keys of any length
79   @param mode           [in]  encryption mode
80   @param iv             [in]  16 bytes initialization vector if needed. Otherwise NULL
81   @param padding        [in]  if padding needed.
82   @return              size of encrypted data, or negative in case of error
83 */
84 
85 int my_aes_encrypt(const unsigned char *source, uint32 source_length,
86                    unsigned char *dest,
87 		   const unsigned char *key, uint32 key_length,
88                    enum my_aes_opmode mode, const unsigned char *iv,
89                    bool padding = true);
90 
91 /**
92   Decrypt an AES encrypted buffer
93 
94   @param source         Pointer to data for decryption
95   @param source_length  size of encrypted data
96   @param dest           buffer to place decrypted data (must be large enough)
97   @param key            Key to be used for decryption
98   @param key_length     Length of the key. Will handle keys of any length
99   @param mode           encryption mode
100   @param iv             16 bytes initialization vector if needed. Otherwise NULL
101   @param padding        if padding needed.
102   @return size of original data.
103 */
104 
105 
106 int my_aes_decrypt(const unsigned char *source, uint32 source_length,
107                    unsigned char *dest,
108                    const unsigned char *key, uint32 key_length,
109                    enum my_aes_opmode mode, const unsigned char *iv,
110                    bool padding = true);
111 
112 /**
113   Calculate the size of a buffer large enough for encrypted data
114 
115   @param source_length  length of data to be encrypted
116   @param mode           encryption mode
117   @return               size of buffer required to store encrypted data
118 */
119 
120 int my_aes_get_size(uint32 source_length, enum my_aes_opmode mode);
121 
122 /**
123   Return true if the AES cipher and block mode requires an IV
124 
125   SYNOPSIS
126   my_aes_needs_iv()
127   @param mode           encryption mode
128 
129   @retval TRUE   IV needed
130   @retval FALSE  IV not needed
131 */
132 
133 my_bool my_aes_needs_iv(my_aes_opmode opmode);
134 
135 
136 C_MODE_END
137 
138 #endif /* MY_AES_INCLUDED */
139