1 /* Copyright (c) 2018 Percona LLC and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
16 
17 #ifndef BINLOG_CRYPT_DATA_H
18 #define BINLOG_CRYPT_DATA_H
19 
20 #include "my_global.h"
21 #include "my_crypt.h"
22 
23 class Binlog_crypt_data
24 {
25 public:
26   enum Binlog_crypt_consts
27   {
28     BINLOG_CRYPTO_SCHEME_LENGTH= 1,
29     BINLOG_KEY_VERSION_LENGTH= 4,
30     BINLOG_IV_LENGTH= MY_AES_BLOCK_SIZE,
31     BINLOG_IV_OFFS_LENGTH= 4,
32     BINLOG_NONCE_LENGTH= BINLOG_IV_LENGTH - BINLOG_IV_OFFS_LENGTH
33   };
34 
35   Binlog_crypt_data();
36   ~Binlog_crypt_data();
37   Binlog_crypt_data(const Binlog_crypt_data &b);
38   Binlog_crypt_data& operator=(Binlog_crypt_data b);
39 
is_enabled()40   bool is_enabled() const
41   {
42     return enabled;
43   }
disable()44   void disable()
45   {
46     enabled= false;
47   }
get_key()48   uchar* get_key() const
49   {
50     return key;
51   }
get_keys_length()52   size_t get_keys_length() const
53   {
54     return key_length;
55   }
get_key_version()56   uint get_key_version() const
57   {
58     return key_version;
59   }
60 
61   void free_key(uchar *&key, size_t &key_length);
62   bool init(uint sch, uint kv, const uchar* nonce);
63   bool init_with_loaded_key(uint sch, const uchar* nonce);
64   bool load_latest_binlog_key();
65   void set_iv(uchar* iv, uint32 offs) const;
66 
67 private:
68   uint  key_version;
69   size_t key_length;
70   uchar *key;
71   uchar nonce[BINLOG_NONCE_LENGTH];
72   uint dst_len;
73   uchar iv[BINLOG_IV_LENGTH];
74   bool enabled;
75   uint scheme;
76 };
77 
78 #endif //BINLOG_CRYPT_DATA_H
79