Skip to content

Commit 432b78c

Browse files
committed
just like tempfiles: use key id 2 for temp Aria tables
introduce ENCRYPTION_KEY_SYSTEM_DATA and ENCRYPTION_KEY_TEMPORARY_DATA constants; use them everywhere.
1 parent d9340d6 commit 432b78c

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

include/mysql/service_encryption.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ extern "C" {
3232
#define ENCRYPTION_KEY_VERSION_INVALID (~(unsigned int)0)
3333
#define ENCRYPTION_KEY_NOT_ENCRYPTED (0)
3434

35+
#define ENCRYPTION_KEY_SYSTEM_DATA 1
36+
#define ENCRYPTION_KEY_TEMPORARY_DATA 2
37+
3538
/* returned from encryption_key_get() */
3639
#define ENCRYPTION_KEY_BUFFER_TOO_SMALL (100)
3740

sql/mf_iocache_encr.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,13 @@ void init_io_cache_encryption()
232232
{
233233
if (encrypt_tmp_files)
234234
{
235-
keyver= encryption_key_get_latest_version(keyid= 2);
235+
keyid= ENCRYPTION_KEY_TEMPORARY_DATA;
236+
keyver= encryption_key_get_latest_version(keyid);
236237
if (keyver == ENCRYPTION_KEY_VERSION_INVALID)
237-
keyver= encryption_key_get_latest_version(keyid= 1);
238+
{
239+
keyid= ENCRYPTION_KEY_SYSTEM_DATA;
240+
keyver= encryption_key_get_latest_version(keyid);
241+
}
238242
}
239243
else
240244
keyver= ENCRYPTION_KEY_VERSION_INVALID;

storage/innobase/include/fil0crypt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Created 04/01/2015 Jan Lindström
2727
#define fil0crypt_h
2828

2929
/* This key will be used if nothing else is given */
30-
#define FIL_DEFAULT_ENCRYPTION_KEY 1
30+
#define FIL_DEFAULT_ENCRYPTION_KEY ENCRYPTION_KEY_SYSTEM_DATA
3131

3232
/** Enum values for encryption table option */
3333
typedef enum {

storage/maria/ma_crypt.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
#include "ma_blockrec.h"
2121
#include <my_crypt.h>
2222

23-
#define HARD_CODED_ENCRYPTION_KEY_ID 1
24-
2523
#define CRYPT_SCHEME_1 1
2624
#define CRYPT_SCHEME_1_ID_LEN 4 /* 4 bytes for counter-block */
2725
#define CRYPT_SCHEME_1_IV_LEN 16
@@ -44,6 +42,24 @@ struct st_maria_crypt_data
4442
mysql_mutex_t lock; /* protecting keys */
4543
};
4644

45+
/**
46+
determine what key id to use for Aria encryption
47+
48+
Same logic as for tempfiles: if key id 2 exists - use it,
49+
otherwise use key id 1.
50+
51+
Key id 1 is system, it always exists. Key id 2 is optional,
52+
it allows to specify fast low-grade encryption for temporary data.
53+
*/
54+
static uint get_encryption_key_id(MARIA_SHARE *share)
55+
{
56+
if (share->options & HA_OPTION_TMP_TABLE &&
57+
encryption_key_id_exists(ENCRYPTION_KEY_TEMPORARY_DATA))
58+
return ENCRYPTION_KEY_TEMPORARY_DATA;
59+
else
60+
return ENCRYPTION_KEY_SYSTEM_DATA;
61+
}
62+
4763
uint
4864
ma_crypt_get_data_page_header_space()
4965
{
@@ -90,7 +106,7 @@ ma_crypt_create(MARIA_SHARE* share)
90106
crypt_data->scheme.type= CRYPT_SCHEME_1;
91107
crypt_data->scheme.locker= crypt_data_scheme_locker;
92108
mysql_mutex_init(key_CRYPT_DATA_lock, &crypt_data->lock, MY_MUTEX_INIT_FAST);
93-
crypt_data->scheme.key_id= HARD_CODED_ENCRYPTION_KEY_ID;
109+
crypt_data->scheme.key_id= get_encryption_key_id(share);
94110
my_random_bytes(crypt_data->scheme.iv, sizeof(crypt_data->scheme.iv));
95111
my_random_bytes((uchar*)&crypt_data->space, sizeof(crypt_data->space));
96112
share->crypt_data= crypt_data;
@@ -156,7 +172,7 @@ ma_crypt_read(MARIA_SHARE* share, uchar *buff)
156172
mysql_mutex_init(key_CRYPT_DATA_lock, &crypt_data->lock,
157173
MY_MUTEX_INIT_FAST);
158174
crypt_data->scheme.locker= crypt_data_scheme_locker;
159-
crypt_data->scheme.key_id= HARD_CODED_ENCRYPTION_KEY_ID;
175+
crypt_data->scheme.key_id= get_encryption_key_id(share);
160176
crypt_data->space= uint4korr(buff + 2);
161177
memcpy(crypt_data->scheme.iv, buff + 6, sizeof(crypt_data->scheme.iv));
162178
share->crypt_data= crypt_data;
@@ -314,7 +330,7 @@ void ma_crypt_set_data_pagecache_callbacks(PAGECACHE_FILE *file,
314330
__attribute__((unused)))
315331
{
316332
/* Only use encryption if we have defined it */
317-
if (encryption_key_id_exists(HARD_CODED_ENCRYPTION_KEY_ID))
333+
if (encryption_key_id_exists(get_encryption_key_id(share)))
318334
{
319335
file->pre_read_hook= ma_crypt_pre_read_hook;
320336
file->post_read_hook= ma_crypt_data_post_read_hook;

storage/xtradb/include/fil0crypt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Created 04/01/2015 Jan Lindström
2727
#define fil0crypt_h
2828

2929
/* This key will be used if nothing else is given */
30-
#define FIL_DEFAULT_ENCRYPTION_KEY 1
30+
#define FIL_DEFAULT_ENCRYPTION_KEY ENCRYPTION_KEY_SYSTEM_DATA
3131

3232
/** Enum values for encryption table option */
3333
typedef enum {

0 commit comments

Comments
 (0)