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 #ifdef __cplusplus
72   #define CPP_DEFAULT_PARAM(v) = v
73 #else
74   #define CPP_DEFAULT_PARAM(v)
75 #endif
76 
77 /**
78   Encrypt a buffer using AES
79 
80   @param source         [in]  Pointer to data for encryption
81   @param source_length  [in]  Size of encryption data
82   @param dest           [out] Buffer to place encrypted data (must be large enough)
83   @param key            [in]  Key to be used for encryption
84   @param key_length     [in]  Length of the key. Will handle keys of any length
85   @param mode           [in]  encryption mode
86   @param iv             [in]  16 bytes initialization vector if needed. Otherwise NULL
87   @param padding        [in]  if padding needed.
88   @return              size of encrypted data, or negative in case of error
89 */
90 
91 int my_aes_encrypt(const unsigned char *source, uint32 source_length,
92                    unsigned char *dest,
93                    const unsigned char *key, uint32 key_length,
94                    enum my_aes_opmode mode, const unsigned char *iv,
95                    my_bool padding CPP_DEFAULT_PARAM(TRUE));
96 
97 /**
98   Decrypt an AES encrypted buffer
99 
100   @param source         Pointer to data for decryption
101   @param source_length  size of encrypted data
102   @param dest           buffer to place decrypted data (must be large enough)
103   @param key            Key to be used for decryption
104   @param key_length     Length of the key. Will handle keys of any length
105   @param mode           encryption mode
106   @param iv             16 bytes initialization vector if needed. Otherwise NULL
107   @param padding        if padding needed.
108   @return size of original data.
109 */
110 
111 
112 int my_aes_decrypt(const unsigned char *source, uint32 source_length,
113                    unsigned char *dest,
114                    const unsigned char *key, uint32 key_length,
115                    enum my_aes_opmode mode, const unsigned char *iv,
116                    my_bool padding CPP_DEFAULT_PARAM(TRUE));
117 
118 /**
119   Calculate the size of a buffer large enough for encrypted data
120 
121   @param source_length  length of data to be encrypted
122   @param mode           encryption mode
123   @return               size of buffer required to store encrypted data
124 */
125 
126 int my_aes_get_size(uint32 source_length, enum my_aes_opmode mode);
127 
128 /**
129   Return true if the AES cipher and block mode requires an IV
130 
131   SYNOPSIS
132   my_aes_needs_iv()
133   @param mode           encryption mode
134 
135   @retval TRUE   IV needed
136   @retval FALSE  IV not needed
137 */
138 
139 my_bool my_aes_needs_iv(enum my_aes_opmode opmode);
140 
141 
142 C_MODE_END
143 
144 #endif /* MY_AES_INCLUDED */
145