MySQL 8.4.3
Source Code Documentation
my_aes.h
Go to the documentation of this file.
1#ifndef MY_AES_INCLUDED
2#define MY_AES_INCLUDED
3
4/* Copyright (c) 2000, 2024, 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 designed to work 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 either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27/**
28 @file include/my_aes.h
29 Wrapper to give simple interface for MySQL to AES standard encryption.
30*/
31
32#include "my_inttypes.h"
33#include "my_macros.h"
34
35#include <string>
36#include <vector>
37
38using std::string;
39using std::vector;
40
41/** AES IV size is 16 bytes for all supported ciphers except ECB */
42#define MY_AES_IV_SIZE 16
43
44/** AES block size is fixed to be 128 bits for CBC and ECB */
45#define MY_AES_BLOCK_SIZE 16
46
47/** Supported AES cipher/block mode combos */
67};
68
69#define MY_AES_BEGIN my_aes_128_ecb
70#define MY_AES_END my_aes_256_ofb
71
72/* If bad data discovered during decoding */
73#define MY_AES_BAD_DATA -1
74
75/** String representations of the supported AES modes. Keep in sync with
76 * my_aes_opmode */
77extern const char *my_aes_opmode_names[];
78
79/**
80 Encrypt a buffer using AES
81
82 @param [in] source Pointer to data for encryption
83 @param [in] source_length Size of encryption data
84 @param [out] dest Buffer to place encrypted data (must be large
85 enough and not overlap with source)
86 @param [in] key Key to be used for encryption
87 @param [in] key_length Length of the key. Will handle keys of any length
88 @param [in] mode encryption mode
89 @param [in] iv 16 bytes initialization vector if needed.
90 Otherwise NULL
91 @param [in] padding if padding needed.
92 @param kdf_options KDF options
93 @return size of encrypted data, or negative in case of error
94*/
95
96int my_aes_encrypt(const unsigned char *source, uint32 source_length,
97 unsigned char *dest, const unsigned char *key,
98 uint32 key_length, enum my_aes_opmode mode,
99 const unsigned char *iv, bool padding = true,
100 vector<string> *kdf_options = nullptr);
101
102/**
103 Decrypt an AES encrypted buffer
104
105 @param source Pointer to data for decryption
106 @param source_length size of encrypted data
107 @param dest buffer to place decrypted data (must be large enough)
108 @param key Key to be used for decryption
109 @param key_length Length of the key. Will handle keys of any length
110 @param mode encryption mode
111 @param iv 16 bytes initialization vector if needed. Otherwise NULL
112 @param padding if padding needed.
113 @param kdf_options KDF options
114 @return size of original data.
115*/
116
117int my_aes_decrypt(const unsigned char *source, uint32 source_length,
118 unsigned char *dest, const unsigned char *key,
119 uint32 key_length, enum my_aes_opmode mode,
120 const unsigned char *iv, bool padding = true,
121 vector<string> *kdf_options = nullptr);
122
123/**
124 Calculate the size of a buffer large enough for encrypted data.
125
126 @param source_length length of data to be encrypted
127 @param opmode encryption mode
128 @return size of buffer required to store encrypted data
129*/
130
131longlong my_aes_get_size(uint32 source_length, enum my_aes_opmode opmode);
132
133/**
134 Return true if the AES cipher and block mode requires an IV.
135
136 @param opmode encryption mode
137
138 @retval true IV needed
139 @retval false IV not needed
140*/
141
142bool my_aes_needs_iv(my_aes_opmode opmode);
143
144#endif /* MY_AES_INCLUDED */
bool my_aes_needs_iv(my_aes_opmode opmode)
Return true if the AES cipher and block mode requires an IV.
Definition: my_aes_openssl.cc:256
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=true, vector< string > *kdf_options=nullptr)
Encrypt a buffer using AES.
Definition: my_aes_openssl.cc:147
my_aes_opmode
Supported AES cipher/block mode combos.
Definition: my_aes.h:48
@ my_aes_256_cbc
Definition: my_aes.h:54
@ my_aes_128_cbc
Definition: my_aes.h:52
@ my_aes_128_cfb128
Definition: my_aes.h:61
@ my_aes_128_ofb
Definition: my_aes.h:64
@ my_aes_256_cfb1
Definition: my_aes.h:57
@ my_aes_128_cfb1
Definition: my_aes.h:55
@ my_aes_192_cfb8
Definition: my_aes.h:59
@ my_aes_192_ofb
Definition: my_aes.h:65
@ my_aes_128_ecb
Definition: my_aes.h:49
@ my_aes_192_cfb1
Definition: my_aes.h:56
@ my_aes_256_ecb
Definition: my_aes.h:51
@ my_aes_192_ecb
Definition: my_aes.h:50
@ my_aes_128_cfb8
Definition: my_aes.h:58
@ my_aes_192_cfb128
Definition: my_aes.h:62
@ my_aes_256_cfb8
Definition: my_aes.h:60
@ my_aes_256_ofb
Definition: my_aes.h:66
@ my_aes_192_cbc
Definition: my_aes.h:53
@ my_aes_256_cfb128
Definition: my_aes.h:63
longlong my_aes_get_size(uint32 source_length, enum my_aes_opmode opmode)
Calculate the size of a buffer large enough for encrypted data.
Definition: my_aes_openssl.cc:245
const char * my_aes_opmode_names[]
String representations of the supported AES modes.
Definition: my_aes_openssl.cc:51
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=true, vector< string > *kdf_options=nullptr)
Decrypt an AES encrypted buffer.
Definition: my_aes_openssl.cc:194
Some integer typedefs for easier portability.
long long int longlong
Definition: my_inttypes.h:55
uint32_t uint32
Definition: my_inttypes.h:67
Some common macros.
mode
Definition: file_handle.h:61
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2875
required string key
Definition: replication_asynchronous_connection_failover.proto:60
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42