log0crypt.h 4.24 KB
Newer Older
1 2 3
/*****************************************************************************

Copyright (C) 2013, 2015, Google Inc. All Rights Reserved.
4
Copyright (C) 2014, 2020, MariaDB Corporation.
5 6 7 8 9 10 11 12 13 14 15

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
16
51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
17 18

*****************************************************************************/
Monty's avatar
Monty committed
19 20 21 22 23
/**************************************************//**
@file include/log0crypt.h
Innodb log encrypt/decrypt

Created 11/25/2013 Minli Zhu
24
Modified           Jan Lindström jan.lindstrom@mariadb.com
25
MDEV-11782: Rewritten for MariaDB 10.2 by Marko Mäkelä, MariaDB Corporation.
Monty's avatar
Monty committed
26 27 28 29
*******************************************************/
#ifndef log0crypt_h
#define log0crypt_h

30
#include "log0log.h"
Monty's avatar
Monty committed
31

32
/** innodb_encrypt_log: whether to encrypt the redo log */
Monty's avatar
Monty committed
33 34
extern my_bool srv_encrypt_log;

35 36 37 38 39
/** Initialize the redo log encryption key and random parameters
when creating a new redo log.
The random parameters will be persisted in the log checkpoint pages.
@see log_crypt_write_checkpoint_buf()
@see log_crypt_read_checkpoint_buf()
40
@return whether the operation succeeded */
Monty's avatar
Monty committed
41
UNIV_INTERN
42 43
bool
log_crypt_init();
Monty's avatar
Monty committed
44 45 46 47 48 49 50 51 52 53 54

/*********************************************************************//**
Writes the crypto (version, msg and iv) info, which has been used for
log blocks with lsn <= this checkpoint's lsn, to a log header's
checkpoint buf. */
UNIV_INTERN
void
log_crypt_write_checkpoint_buf(
/*===========================*/
	byte*	buf);			/*!< in/out: checkpoint buffer */

55 56 57
/** Read the MariaDB 10.1 checkpoint crypto (version, msg and iv) info.
@param[in]	buf	checkpoint buffer
@return	whether the operation was successful */
58
ATTRIBUTE_COLD bool log_crypt_101_read_checkpoint(const byte* buf);
59 60

/** Decrypt a MariaDB 10.1 redo log block.
61 62
@param[in,out]	buf		log block
@param[in]	start_lsn	server start LSN
63
@return	whether the decryption was successful */
64
ATTRIBUTE_COLD bool log_crypt_101_read_block(byte* buf, lsn_t start_lsn);
65

66 67 68
/** Read the checkpoint crypto (version, msg and iv) info.
@param[in]	buf	checkpoint buffer
@return	whether the operation was successful */
69
bool log_crypt_read_checkpoint_buf(const byte* buf);
70

71 72 73 74 75 76 77 78 79 80
/** log_crypt() operation code */
enum log_crypt_t {
	/** encrypt a log block without rotating key */
	LOG_ENCRYPT,
	/** decrypt a log block */
	LOG_DECRYPT,
	/** attempt to rotate the key, and encrypt a log block */
	LOG_ENCRYPT_ROTATE_KEY
};

81 82
/** Encrypt or decrypt log blocks.
@param[in,out]	buf	log blocks to encrypt or decrypt
83
@param[in]	lsn	log sequence number of the start of the buffer
84
@param[in]	size	size of the buffer, in bytes
85 86 87
@param[in]	op	whether to decrypt, encrypt, or rotate key and encrypt
@return	whether the operation succeeded (encrypt always does) */
bool log_crypt(byte* buf, lsn_t lsn, ulint size, log_crypt_t op = LOG_ENCRYPT);
88

Marko Mäkelä's avatar
Marko Mäkelä committed
89 90
/** Encrypt or decrypt a temporary file block.
@param[in]	src		block to encrypt or decrypt
91
@param[in]	size		size of the block
Marko Mäkelä's avatar
Marko Mäkelä committed
92
@param[out]	dst		destination block
93
@param[in]	offs		offset to block
Marko Mäkelä's avatar
Marko Mäkelä committed
94 95
@param[in]	encrypt		true=encrypt; false=decrypt
@return whether the operation succeeded */
96 97 98
UNIV_INTERN
bool
log_tmp_block_encrypt(
Marko Mäkelä's avatar
Marko Mäkelä committed
99 100 101 102 103 104 105 106 107
	const byte*	src,
	ulint		size,
	byte*		dst,
	uint64_t	offs,
	bool		encrypt = true)
	MY_ATTRIBUTE((warn_unused_result, nonnull));

/** Decrypt a temporary file block.
@param[in]	src		block to decrypt
108
@param[in]	size		size of the block
Marko Mäkelä's avatar
Marko Mäkelä committed
109
@param[out]	dst		destination block
110
@param[in]	offs		offset to block
Marko Mäkelä's avatar
Marko Mäkelä committed
111 112
@return whether the operation succeeded */
inline
113 114
bool
log_tmp_block_decrypt(
Marko Mäkelä's avatar
Marko Mäkelä committed
115 116 117
	const byte*	src,
	ulint		size,
	byte*		dst,
118
	uint64_t	offs)
Marko Mäkelä's avatar
Marko Mäkelä committed
119
{
120
	return(log_tmp_block_encrypt(src, size, dst, offs, false));
Marko Mäkelä's avatar
Marko Mäkelä committed
121 122 123 124
}

/** @return whether temporary files are encrypted */
inline bool log_tmp_is_encrypted() { return srv_encrypt_log; }
Monty's avatar
Monty committed
125
#endif  // log0crypt.h