MySQL 9.5.0
Source Code Documentation
stream_cipher.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef STREAM_CIPHER_INCLUDED
25#define STREAM_CIPHER_INCLUDED
26
27#include <openssl/evp.h> // IWYU pragma: keep
28// IWYU pragma: no_include <openssl/types.h>
29#include <cstdint>
30#include <memory>
31#include <string>
32#include "my_ssl_algo_cache.h"
33
34#include "my_char_traits.h"
35
36/**
37 @file stream_cipher.h
38
39 @brief This file includes core components for encrypting/decrypting
40 binary log files.
41*/
42
44 std::basic_string<unsigned char, my_char_traits<unsigned char>>;
45
46/**
47 @class Stream_cipher
48
49 This abstract class represents the interface of a replication logs encryption
50 cipher that can be used to encrypt/decrypt a given stream content in both
51 sequential and random way.
52
53 - Sequential means encrypting/decrypting a stream from the begin to end
54 in order. For sequential encrypting/decrypting, you just need to call
55 it like:
56
57 open();
58 encrypt();
59 ...
60 encrypt(); // call it again and again
61 ...
62 close();
63
64 - Random means encrypting/decrypting a stream data without order. For
65 example:
66
67 - It first encrypts the data of a stream at the offset from 100 to 200.
68
69 - And then encrypts the data of the stream at the offset from 0 to 99.
70
71 For random encrypting/decrypting, you need to call set_stream_offset()
72 before calling encrypt(). Example:
73
74 open();
75
76 set_stream_offset(100);
77 encrypt(...);
78 ...
79 set_stream_offset(0);
80 encrypt(...)
81
82 close();
83*/
85 public:
86 virtual ~Stream_cipher() = default;
87
88 /**
89 Open the cipher with given password.
90
91 @param[in] password The password which is used to initialize the cipher.
92 @param[in] header_size The encrypted stream offset wrt the down stream.
93
94 @retval false Success.
95 @retval true Error.
96 */
97 virtual bool open(const Key_string &password, int header_size) = 0;
98
99 /** Close the cipher. */
100 virtual void close() = 0;
101
102 /**
103 Encrypt data.
104
105 @param[in] dest The buffer for storing encrypted data. It should be
106 at least 'length' bytes.
107 @param[in] src The data which will be encrypted.
108 @param[in] length Length of the data.
109
110 @retval false Success.
111 @retval true Error.
112 */
113 virtual bool encrypt(unsigned char *dest, const unsigned char *src,
114 int length) = 0;
115
116 /**
117 Decrypt data.
118
119 @param[in] dest The buffer for storing decrypted data. It should be
120 at least 'length' bytes.
121 @param[in] src The data which will be decrypted.
122 @param[in] length Length of the data.
123
124 @retval false Success.
125 @retval true Error.
126 */
127 virtual bool decrypt(unsigned char *dest, const unsigned char *src,
128 int length) = 0;
129
130 /**
131 Support encrypting/decrypting data at random position of a stream.
132
133 @param[in] offset The stream offset of the data which will be encrypted/
134 decrypted in next encrypt()/decrypt() call.
135
136 @retval false Success.
137 @retval true Error.
138 */
139 virtual bool set_stream_offset(uint64_t offset) = 0;
140
141 /**
142 Returns the size of the header of the stream being encrypted/decrypted.
143
144 @return the size of the header of the stream being encrypted/decrypted.
145 */
146 int get_header_size();
147
148 protected:
150};
151
152/**
153 @class Aes_ctr
154
155 The class provides standards to be used by the Aes_ctr ciphers.
156*/
157class Aes_ctr {
158 public:
159 static const int PASSWORD_LENGTH = 32;
160 static const int AES_BLOCK_SIZE = 16;
161 static const int FILE_KEY_LENGTH = 32;
162 /**
163 Returns the message digest function to be uses when opening the cipher.
164
165 @return SHA-512 message digest.
166 */
167 static const EVP_MD *get_evp_md() { return my_EVP_sha512(); }
168 /**
169 Returns the cipher to be uses when using the cipher.
170
171 @return AES-256-CTR.
172 */
173 static const EVP_CIPHER *get_evp_cipher() { return my_EVP_aes_256_ctr(); }
174 /**
175 Returns a new unique Stream_cipher encryptor.
176
177 @return A new Stream_cipher encryptor.
178 */
179 static std::unique_ptr<Stream_cipher> get_encryptor();
180 /**
181 Returns a new unique Stream_cipher decryptor.
182
183 @return A new Stream_cipher decryptor.
184 */
185 static std::unique_ptr<Stream_cipher> get_decryptor();
186};
187
188enum class Cipher_type : int { ENCRYPT = 0, DECRYPT = 1 };
189
190/**
191 @class Aes_ctr_cipher
192
193 The class implements AES-CTR encryption/decryption. It supports to
194 encrypt/decrypt a stream in both sequential and random way.
195*/
196template <Cipher_type TYPE>
198 public:
202
203 ~Aes_ctr_cipher() override;
204
205 bool open(const Key_string &password, int header_size) override;
206 void close() override;
207 bool encrypt(unsigned char *dest, const unsigned char *src,
208 int length) override;
209 bool decrypt(unsigned char *dest, const unsigned char *src,
210 int length) override;
211 bool set_stream_offset(uint64_t offset) override;
212
213 private:
214 /* Cipher context */
215 EVP_CIPHER_CTX *m_ctx = nullptr;
216 /* The file key to encrypt/decrypt data. */
218 /* The initialization vector (IV) used to encrypt/decrypt data. */
219 unsigned char m_iv[AES_BLOCK_SIZE];
220
221 /**
222 Initialize OpenSSL cipher related context and IV.
223
224 @param[in] offset The stream offset to compute the AES-CTR counter which
225 will be set into IV.
226
227 @retval false Success.
228 @retval true Error.
229 */
230 bool init_cipher(uint64_t offset);
231
232 /** Destroy OpenSSL cipher related context. */
233 void deinit_cipher();
234};
235
237typedef class Aes_ctr_cipher<Cipher_type::DECRYPT> Aes_ctr_decryptor;
238#endif // STREAM_CIPHER_INCLUDED
The class implements AES-CTR encryption/decryption.
Definition: stream_cipher.h:197
static const int PASSWORD_LENGTH
Definition: stream_cipher.h:199
bool open(const Key_string &password, int header_size) override
Open the cipher with given password.
Definition: stream_cipher.cc:46
static const int FILE_KEY_LENGTH
Definition: stream_cipher.h:201
unsigned char m_file_key[FILE_KEY_LENGTH]
Definition: stream_cipher.h:217
bool encrypt(unsigned char *dest, const unsigned char *src, int length) override
Encrypt data.
Definition: stream_cipher.cc:143
void deinit_cipher()
Destroy OpenSSL cipher related context.
Definition: stream_cipher.cc:137
unsigned char m_iv[AES_BLOCK_SIZE]
Definition: stream_cipher.h:219
static const int AES_BLOCK_SIZE
Definition: stream_cipher.h:200
~Aes_ctr_cipher() override
Definition: stream_cipher.cc:73
bool decrypt(unsigned char *dest, const unsigned char *src, int length) override
Decrypt data.
Definition: stream_cipher.cc:163
bool set_stream_offset(uint64_t offset) override
Support encrypting/decrypting data at random position of a stream.
Definition: stream_cipher.cc:83
bool init_cipher(uint64_t offset)
Initialize OpenSSL cipher related context and IV.
Definition: stream_cipher.cc:105
EVP_CIPHER_CTX * m_ctx
Definition: stream_cipher.h:215
void close() override
Close the cipher.
Definition: stream_cipher.cc:78
The class provides standards to be used by the Aes_ctr ciphers.
Definition: stream_cipher.h:157
static const int AES_BLOCK_SIZE
Definition: stream_cipher.h:160
static std::unique_ptr< Stream_cipher > get_encryptor()
Returns a new unique Stream_cipher encryptor.
Definition: stream_cipher.cc:35
static std::unique_ptr< Stream_cipher > get_decryptor()
Returns a new unique Stream_cipher decryptor.
Definition: stream_cipher.cc:40
static const int PASSWORD_LENGTH
Definition: stream_cipher.h:159
static const EVP_MD * get_evp_md()
Returns the message digest function to be uses when opening the cipher.
Definition: stream_cipher.h:167
static const int FILE_KEY_LENGTH
Definition: stream_cipher.h:161
static const EVP_CIPHER * get_evp_cipher()
Returns the cipher to be uses when using the cipher.
Definition: stream_cipher.h:173
This abstract class represents the interface of a replication logs encryption cipher that can be used...
Definition: stream_cipher.h:84
virtual bool decrypt(unsigned char *dest, const unsigned char *src, int length)=0
Decrypt data.
int get_header_size()
Returns the size of the header of the stream being encrypted/decrypted.
Definition: stream_cipher.cc:33
virtual bool set_stream_offset(uint64_t offset)=0
Support encrypting/decrypting data at random position of a stream.
virtual bool open(const Key_string &password, int header_size)=0
Open the cipher with given password.
int m_header_size
Definition: stream_cipher.h:149
virtual void close()=0
Close the cipher.
virtual bool encrypt(unsigned char *dest, const unsigned char *src, int length)=0
Encrypt data.
virtual ~Stream_cipher()=default
const EVP_MD * my_EVP_sha512()
Definition: my_ssl_algo_cache.cc:105
const EVP_CIPHER * my_EVP_aes_256_ctr()
Definition: my_ssl_algo_cache.cc:273
static char * password
Definition: mysql_secure_installation.cc:58
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
class Aes_ctr_cipher< Cipher_type::ENCRYPT > Aes_ctr_encryptor
Definition: stream_cipher.h:236
std::basic_string< unsigned char, my_char_traits< unsigned char > > Key_string
Definition: stream_cipher.h:44
Cipher_type
Definition: stream_cipher.h:188
class Aes_ctr_cipher< Cipher_type::DECRYPT > Aes_ctr_decryptor
Definition: stream_cipher.h:237