Skip to content

Commit d7d589d

Browse files
montywivuvova
authored andcommitted
Push for testing of encryption
1 parent 3a3ec74 commit d7d589d

File tree

273 files changed

+23385
-1094
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

273 files changed

+23385
-1094
lines changed

include/maria.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ extern my_bool maria_delay_key_write;
268268
extern my_off_t maria_max_temp_length;
269269
extern ulong maria_bulk_insert_tree_size, maria_data_pointer_size;
270270
extern MY_TMPDIR *maria_tmpdir;
271+
extern my_bool maria_encrypt_tables;
272+
271273
/*
272274
This is used to check if a symlink points into the mysql data home,
273275
which is normally forbidden as it can be used to get access to

include/my_aes.h

Lines changed: 168 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#ifndef MY_AES_INCLUDED
2-
#define MY_AES_INCLUDED
3-
41
/* Copyright (c) 2002, 2006 MySQL AB, 2009 Sun Microsystems, Inc.
52
Use is subject to license terms.
63
@@ -21,47 +18,192 @@
2118
/* Header file for my_aes.c */
2219
/* Wrapper to give simple interface for MySQL to AES standard encryption */
2320

21+
#ifndef MY_AES_INCLUDED
22+
#define MY_AES_INCLUDED
23+
24+
/* We expect same result code from encryption functions as in my_aes.h */
25+
typedef int Crypt_result;
26+
27+
#define AES_OK 0
28+
#define AES_BAD_DATA -1
29+
#define AES_BAD_IV -2
30+
#define AES_INVALID -3
31+
#define AES_OPENSSL_ERROR -4
32+
#define AES_BAD_KEYSIZE -5
33+
#define AES_KEY_CREATION_FAILED -10
34+
35+
#define CRYPT_KEY_OK 0
36+
#define CRYPT_BUFFER_TO_SMALL -11;
37+
#define CRYPT_KEY_UNKNOWN -48;
38+
39+
/* The max block sizes of all supported algorithms */
40+
#define MY_AES_BLOCK_SIZE 16
41+
42+
/* The max key length of all supported algorithms */
43+
#define MY_AES_MAX_KEY_LENGTH 32
44+
45+
2446
#include "rijndael.h"
2547

2648
C_MODE_START
2749

2850
#define AES_KEY_LENGTH 128/* Must be 128 192 or 256 */
2951

30-
/*
31-
my_aes_encrypt - Crypt buffer with AES encryption algorithm.
32-
source - Pointer to data for encryption
33-
source_length - size of encryption data
34-
dest - buffer to place encrypted data (must be large enough)
35-
key - Key to be used for encryption
36-
kel_length - Length of the key. Will handle keys of any length
52+
/**
53+
Crypt buffer with AES dynamic (defined at startup) encryption algorithm.
54+
55+
SYNOPSIS
56+
my_aes_encrypt_dynamic()
57+
@param source [in] Pointer to data for encryption
58+
@param source_length [in] Size of encryption data
59+
@param dest [out] Buffer to place encrypted data (must be large enough)
60+
@param dest_length [out] Pointer to size of encrypted data
61+
@param key [in] Key to be used for encryption
62+
@param key_length [in] Length of the key. 16, 24 or 32
63+
@param iv [in] Iv to be used for encryption
64+
@param iv_length [in] Length of the iv. should be 16.
65+
@param noPadding [in] if set, algorithm specific padding behaviour is used
66+
67+
Method used defined by calling my_aes_init_dynamic_encrypt() at startup.
68+
69+
@return
70+
!= 0 error
71+
0 no error
72+
*/
73+
74+
typedef int (*my_aes_encrypt_dynamic_type)(const uchar* source, uint32 source_length,
75+
uchar* dest, uint32* dest_length,
76+
const uchar* key, uint8 key_length,
77+
const uchar* iv, uint8 iv_length,
78+
uint noPadding);
79+
80+
extern my_aes_encrypt_dynamic_type my_aes_encrypt_dynamic;
81+
82+
/**
83+
AES decryption AES dynamic (defined at startup) encryption algorithm.
84+
85+
SYNOPSIS
86+
my_aes_decrypt_dynamic()
87+
@param source [in] Pointer to data to decrypt
88+
@param source_length [in] Size of data
89+
@param dest [out] Buffer to place decrypted data (must be large enough)
90+
@param dest_length [out] Pointer to size of decrypted data
91+
@param key [in] Key to be used for decryption
92+
@param key_length [in] Length of the key. 16, 24 or 32
93+
@param iv [in] Iv to be used for encryption
94+
@param iv_length [in] Length of the iv. should be 16.
95+
@param noPadding [in] if set, algorithm specific padding behaviour is used
96+
97+
@return
98+
!= 0 error
99+
0 no error
100+
101+
Method used defined by calling my_aes_init_dynamic_encrypt() at startup.
102+
*/
103+
104+
typedef int (*my_aes_decrypt_dynamic_type)(const uchar *source,
105+
uint32 source_length,
106+
uchar *dest, uint32 *dest_length,
107+
const uchar *key, uint8 key_length,
108+
const uchar *iv, uint8 iv_length,
109+
uint noPadding);
110+
extern my_aes_decrypt_dynamic_type my_aes_decrypt_dynamic;
111+
112+
/**
113+
Initialize dynamic crypt functions
114+
*/
115+
116+
enum enum_my_aes_encryption_algorithm
117+
{
118+
MY_AES_ALGORITHM_NONE, MY_AES_ALGORITHM_ECB, MY_AES_ALGORITHM_CBC,
119+
MY_AES_ALGORITHM_CTR
120+
};
37121

38-
returns - size of encrypted data, or negative in case of error.
122+
my_aes_decrypt_dynamic_type get_aes_decrypt_func(enum enum_my_aes_encryption_algorithm method);
123+
my_aes_encrypt_dynamic_type get_aes_encrypt_func(enum enum_my_aes_encryption_algorithm method);
124+
125+
126+
my_bool my_aes_init_dynamic_encrypt(enum enum_my_aes_encryption_algorithm method);
127+
128+
extern MYSQL_PLUGIN_IMPORT enum enum_my_aes_encryption_algorithm current_aes_dynamic_method;
129+
130+
131+
132+
/**
133+
Calculate key and iv from a given salt and secret as it is handled in openssl
134+
encrypted files via console
135+
136+
SYNOPSIS
137+
my_bytes_to_key()
138+
139+
@param salt [in] the given salt as extracted from the encrypted file
140+
@param secret [in] the given secret as String, provided by the user
141+
@param key [out] 32 Bytes of key are written to this pointer
142+
@param iv [out] 16 Bytes of iv are written to this pointer
143+
*/
144+
145+
void my_bytes_to_key(const uchar *salt,
146+
const char *secret, uchar *key,
147+
uchar *iv);
148+
149+
/**
150+
Decode Hexencoded String to uint8[].
151+
152+
SYNOPSIS
153+
my_aes_hex2uint()
154+
@param iv [in] Pointer to hexadecimal encoded IV String
155+
@param dest [out] Pointer to output uint8 array. Memory needs to be
156+
allocated by caller
157+
@param iv_length [in] Size of destination array.
158+
*/
159+
160+
void my_aes_hex2uint(const char *in, uchar *out, int dest_length);
161+
162+
/**
163+
Crypt buffer with AES encryption algorithm.
164+
165+
SYNOPSIS
166+
my_aes_encrypt()
167+
168+
@param source Pointer to data for encryption
169+
@param source_length Size of encryption data
170+
@param dest Buffer to place encrypted data (must be large enough)
171+
@param key Key to be used for encryption
172+
@param kel_length Length of the key. Will handle keys of any length
173+
174+
@return Size of encrypted data, or negative in case of error.
39175
*/
40176

41-
int my_aes_encrypt(const char *source, int source_length, char *dest,
177+
int my_aes_encrypt(const uchar *source, int source_length, uchar *dest,
42178
const char *key, int key_length);
43179

44-
/*
45-
my_aes_decrypt - DeCrypt buffer with AES encryption algorithm.
46-
source - Pointer to data for decryption
47-
source_length - size of encrypted data
48-
dest - buffer to place decrypted data (must be large enough)
49-
key - Key to be used for decryption
50-
kel_length - Length of the key. Will handle keys of any length
180+
/**
181+
DeCrypt buffer with AES encryption algorithm.
182+
183+
SYNOPSIS
184+
my_aes_decrypt()
51185
52-
returns - size of original data, or negative in case of error.
186+
@param source Pointer to data for decryption
187+
@param source_length size of encrypted data
188+
@param dest buffer to place decrypted data (must be large enough)
189+
@param key Key to be used for decryption
190+
@param kel_length Length of the key. Will handle keys of any length
191+
192+
@return size of original data, or negative in case of error.
53193
*/
54194

55195

56-
int my_aes_decrypt(const char *source, int source_length, char *dest,
196+
int my_aes_decrypt(const uchar *source, int source_length, uchar *dest,
57197
const char *key, int key_length);
58198

59-
/*
60-
my_aes_get_size - get size of buffer which will be large enough for encrypted
61-
data
62-
source_length - length of data to be encrypted
199+
/**
200+
get size of buffer which will be large enough for encrypted data
201+
202+
SYNOPSIS
203+
my_aes_get_size()
204+
@param source_length Length of data to be encrypted
63205
64-
returns - size of buffer required to store encrypted data
206+
@return Size of buffer required to store encrypted data
65207
*/
66208

67209
int my_aes_get_size(int source_length);

include/my_base.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ enum ha_base_keytype {
354354
#define HA_CREATE_DELAY_KEY_WRITE 64
355355
#define HA_CREATE_RELIES_ON_SQL_LAYER 128
356356
#define HA_CREATE_INTERNAL_TABLE 256
357+
#define HA_CREATE_ENCRYPTED 512
358+
#define HA_INSERT_ORDER 1024
357359

358360
/* Flags used by start_bulk_insert */
359361

include/my_crypt.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// TODO: Add Windows support
2+
3+
#ifndef MYSYS_MY_CRYPT_H_
4+
#define MYSYS_MY_CRYPT_H_
5+
6+
#include <my_aes.h>
7+
8+
#if !defined(HAVE_YASSL) && defined(HAVE_OPENSSL)
9+
10+
#define HAVE_EncryptAes128Ctr
11+
12+
C_MODE_START
13+
Crypt_result my_aes_encrypt_ctr(const uchar* source, uint32 source_length,
14+
uchar* dest, uint32* dest_length,
15+
const unsigned char* key, uint8 key_length,
16+
const unsigned char* iv, uint8 iv_length,
17+
uint noPadding);
18+
19+
Crypt_result my_aes_decrypt_ctr(const uchar* source, uint32 source_length,
20+
uchar* dest, uint32* dest_length,
21+
const unsigned char* key, uint8 key_length,
22+
const unsigned char* iv, uint8 iv_length,
23+
uint noPadding);
24+
C_MODE_END
25+
26+
Crypt_result EncryptAes128Ctr(const uchar* key,
27+
const uchar* iv, int iv_size,
28+
const uchar* plaintext, int plaintext_size,
29+
uchar* ciphertext, int* ciphertext_used);
30+
31+
Crypt_result DecryptAes128Ctr(const uchar* key,
32+
const uchar* iv, int iv_size,
33+
const uchar* ciphertext, int ciphertext_size,
34+
uchar* plaintext, int* plaintext_used);
35+
36+
#endif /* !defined(HAVE_YASSL) && defined(HAVE_OPENSSL) */
37+
38+
C_MODE_START
39+
Crypt_result my_random_bytes(uchar* buf, int num);
40+
C_MODE_END
41+
42+
#endif /* MYSYS_MY_CRYPT_H_ */

include/my_crypt_key_management.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
#ifndef MYSYS_MY_CRYPT_KEY_MANAGMENT_H_
3+
#define MYSYS_MY_CRYPT_KEY_MANAGMENT_H_
4+
5+
#include "my_global.h"
6+
#include "my_pthread.h"
7+
#include "mysql/psi/psi.h"
8+
9+
#ifndef DBUG_OFF
10+
extern my_bool debug_use_static_encryption_keys;
11+
12+
#ifdef HAVE_PSI_INTERFACE
13+
extern PSI_rwlock_key key_LOCK_dbug_encryption_key_version;
14+
#endif
15+
16+
extern mysql_rwlock_t LOCK_dbug_encryption_key_version;
17+
extern uint opt_debug_encryption_key_version;
18+
#endif /* DBUG_OFF */
19+
20+
C_MODE_START
21+
/**
22+
* function returning latest key version
23+
*/
24+
typedef int (* GetLatestCryptoKeyVersionFunc_t)();
25+
26+
/**
27+
* function returning if the key exists
28+
*/
29+
typedef unsigned int (* HasKeyVersionFunc_t)(unsigned int version);
30+
31+
/**
32+
* function returning the key size
33+
*/
34+
typedef int (* GetKeySizeFunc_t)(unsigned int version);
35+
36+
/**
37+
* function returning a key for a key version
38+
*/
39+
typedef int (* GetCryptoKeyFunc_t)(unsigned int version,
40+
unsigned char* key,
41+
unsigned keybufsize);
42+
43+
/**
44+
* function returning an iv for a key version
45+
*/
46+
typedef int (* GetCryptoIVFunc_t)(unsigned int version,
47+
unsigned char* iv,
48+
unsigned ivbufsize);
49+
50+
51+
struct CryptoKeyFuncs_t
52+
{
53+
GetLatestCryptoKeyVersionFunc_t getLatestCryptoKeyVersionFunc;
54+
HasKeyVersionFunc_t hasCryptoKeyFunc;
55+
GetKeySizeFunc_t getCryptoKeySize;
56+
GetCryptoKeyFunc_t getCryptoKeyFunc;
57+
GetCryptoIVFunc_t getCryptoIVFunc;
58+
};
59+
60+
/**
61+
* Install functions to use for key management
62+
*/
63+
void
64+
InstallCryptoKeyFunctions(const struct CryptoKeyFuncs_t* cryptoKeyFuncs);
65+
66+
/**
67+
* Functions to interact with key management
68+
*/
69+
70+
int GetLatestCryptoKeyVersion();
71+
unsigned int HasCryptoKey(unsigned int version);
72+
int GetCryptoKeySize(unsigned int version);
73+
int GetCryptoKey(unsigned int version, unsigned char* key_buffer,
74+
unsigned int size);
75+
int GetCryptoIV(unsigned int version, unsigned char* key_buffer,
76+
unsigned int size);
77+
78+
C_MODE_END
79+
80+
#endif // MYSYS_MY_CRYPT_KEY_MANAGMENT_H_

include/my_dbug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ extern void _db_suicide_();
176176
#define DBUG_SYNC_POINT(lock_name,lock_timeout) \
177177
debug_sync_point(lock_name,lock_timeout)
178178
void debug_sync_point(const char* lock_name, uint lock_timeout);
179+
180+
/* Extern function for debugging */
181+
extern void dump_buffer(FILE *stream, unsigned n, const unsigned char* buf);
179182
#else
180183
#define DBUG_SYNC_POINT(lock_name,lock_timeout)
181184
#endif /* EXTRA_DEBUG */

include/my_md5.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
extern "C" {
2929
#endif
3030

31-
#define compute_md5_hash(A,B,C) my_md5(A,B,C)
31+
#define compute_md5_hash(A,B,C) my_md5((unsigned char *)A,B,C)
3232

3333
/*
3434
Convert an array of bytes to a hexadecimal representation.

include/mysql/plugin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ typedef struct st_mysql_xid MYSQL_XID;
8888
#define MYSQL_AUDIT_PLUGIN 5
8989
#define MYSQL_REPLICATION_PLUGIN 6
9090
#define MYSQL_AUTHENTICATION_PLUGIN 7
91-
#define MYSQL_MAX_PLUGIN_TYPE_NUM 9 /* The number of plugin types */
91+
#define MYSQL_KEY_MANAGEMENT_PLUGIN 9
92+
#define MYSQL_MAX_PLUGIN_TYPE_NUM 10 /* The number of plugin types */
9293

9394
/* MariaDB plugin types */
9495
#define MariaDB_PASSWORD_VALIDATION_PLUGIN 8

0 commit comments

Comments
 (0)