PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/storage/xtradb/include/fil0crypt.h

https://gitlab.com/Cona/server
C Header | 431 lines | 231 code | 50 blank | 150 comment | 0 complexity | dfb588ad7b3c0b95ebfca381fb88795a MD5 | raw file
  1. /*****************************************************************************
  2. Copyright (C) 2013, 2015, Google Inc. All Rights Reserved.
  3. Copyright (c) 2015, 2016, MariaDB Corporation.
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  13. *****************************************************************************/
  14. /**************************************************//**
  15. @file include/fil0crypt.h
  16. The low-level file system encryption support functions
  17. Created 04/01/2015 Jan Lindström
  18. *******************************************************/
  19. #ifndef fil0crypt_h
  20. #define fil0crypt_h
  21. /**
  22. * Magic pattern in start of crypt data on page 0
  23. */
  24. #define MAGIC_SZ 6
  25. static const unsigned char CRYPT_MAGIC[MAGIC_SZ] = {
  26. 's', 0xE, 0xC, 'R', 'E', 't' };
  27. static const unsigned char EMPTY_PATTERN[MAGIC_SZ] = {
  28. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
  29. /* This key will be used if nothing else is given */
  30. #define FIL_DEFAULT_ENCRYPTION_KEY ENCRYPTION_KEY_SYSTEM_DATA
  31. /** Enum values for encryption table option */
  32. typedef enum {
  33. FIL_SPACE_ENCRYPTION_DEFAULT = 0, /* Tablespace encrypted if
  34. srv_encrypt_tables = ON */
  35. FIL_SPACE_ENCRYPTION_ON = 1, /* Tablespace is encrypted always */
  36. FIL_SPACE_ENCRYPTION_OFF = 2 /* Tablespace is not encrypted */
  37. } fil_encryption_t;
  38. /**
  39. * CRYPT_SCHEME_UNENCRYPTED
  40. *
  41. * Used as intermediate state when convering a space from unencrypted
  42. * to encrypted
  43. */
  44. /**
  45. * CRYPT_SCHEME_1
  46. *
  47. * xxx is AES_CTR or AES_CBC (or another block cypher with the same key and iv lengths)
  48. * L = AES_ECB(KEY, IV)
  49. * CRYPT(PAGE) = xxx(KEY=L, IV=C, PAGE)
  50. */
  51. #define CRYPT_SCHEME_1 1
  52. #define CRYPT_SCHEME_1_IV_LEN 16
  53. #define CRYPT_SCHEME_UNENCRYPTED 0
  54. /* Cached L or key for given key_version */
  55. struct key_struct
  56. {
  57. uint key_version; /*!< Version of the key */
  58. uint key_length; /*!< Key length */
  59. unsigned char key[MY_AES_MAX_KEY_LENGTH]; /*!< Cached key
  60. (that is L in CRYPT_SCHEME_1) */
  61. };
  62. struct fil_space_rotate_state_t
  63. {
  64. time_t start_time; /*!< time when rotation started */
  65. ulint active_threads; /*!< active threads in space */
  66. ulint next_offset; /*!< next "free" offset */
  67. ulint max_offset; /*!< max offset needing to be rotated */
  68. uint min_key_version_found; /*!< min key version found but not
  69. rotated */
  70. lsn_t end_lsn; /*!< max lsn created when rotating this
  71. space */
  72. bool starting; /*!< initial write of IV */
  73. bool flushing; /*!< space is being flushed at end of rotate */
  74. struct {
  75. bool is_active; /*!< is scrubbing active in this space */
  76. time_t last_scrub_completed; /*!< when was last scrub
  77. completed */
  78. } scrubbing;
  79. };
  80. struct fil_space_crypt_struct : st_encryption_scheme
  81. {
  82. uint min_key_version; // min key version for this space
  83. ulint page0_offset; // byte offset on page 0 for crypt data
  84. fil_encryption_t encryption; // Encryption setup
  85. ib_mutex_t mutex; // mutex protecting following variables
  86. bool closing; // is tablespace being closed
  87. bool inited;
  88. fil_space_rotate_state_t rotate_state;
  89. };
  90. /* structure containing encryption specification */
  91. typedef struct fil_space_crypt_struct fil_space_crypt_t;
  92. /*********************************************************************
  93. Init global resources needed for tablespace encryption/decryption */
  94. UNIV_INTERN
  95. void
  96. fil_space_crypt_init();
  97. /*********************************************************************
  98. Cleanup global resources needed for tablespace encryption/decryption */
  99. UNIV_INTERN
  100. void
  101. fil_space_crypt_cleanup();
  102. /*********************************************************************
  103. Create crypt data, i.e data that is used for a single tablespace */
  104. UNIV_INTERN
  105. fil_space_crypt_t *
  106. fil_space_create_crypt_data(
  107. /*========================*/
  108. fil_encryption_t encrypt_mode, /*!< in: encryption mode */
  109. uint key_id); /*!< in: encryption key id */
  110. /*********************************************************************
  111. Destroy crypt data */
  112. UNIV_INTERN
  113. void
  114. fil_space_destroy_crypt_data(
  115. /*=========================*/
  116. fil_space_crypt_t **crypt_data); /*!< in/out: crypt data */
  117. /*********************************************************************
  118. Get crypt data for a space*/
  119. UNIV_INTERN
  120. fil_space_crypt_t *
  121. fil_space_get_crypt_data(
  122. /*=====================*/
  123. ulint space); /*!< in: tablespace id */
  124. /*********************************************************************
  125. Set crypt data for a space*/
  126. UNIV_INTERN
  127. fil_space_crypt_t*
  128. fil_space_set_crypt_data(
  129. /*=====================*/
  130. ulint space, /*!< in: tablespace id */
  131. fil_space_crypt_t* crypt_data); /*!< in: crypt data to set */
  132. /*********************************************************************
  133. Merge crypt data */
  134. UNIV_INTERN
  135. void
  136. fil_space_merge_crypt_data(
  137. /*=======================*/
  138. fil_space_crypt_t* dst_crypt_data, /*!< in: crypt_data */
  139. const fil_space_crypt_t* src_crypt_data); /*!< in: crypt data */
  140. /*********************************************************************
  141. Read crypt data from buffer page */
  142. UNIV_INTERN
  143. fil_space_crypt_t *
  144. fil_space_read_crypt_data(
  145. /*======================*/
  146. ulint space, /*!< in: tablespace id */
  147. const byte* page, /*!< in: buffer page */
  148. ulint offset); /*!< in: offset where crypt data is stored */
  149. /*********************************************************************
  150. Write crypt data to buffer page */
  151. UNIV_INTERN
  152. void
  153. fil_space_write_crypt_data(
  154. /*=======================*/
  155. ulint space, /*!< in: tablespace id */
  156. byte* page, /*!< in: buffer page */
  157. ulint offset, /*!< in: offset where to store data */
  158. ulint maxsize, /*!< in: max space available to store crypt data in */
  159. mtr_t * mtr); /*!< in: mini-transaction */
  160. /*********************************************************************
  161. Clear crypt data from page 0 (used for import tablespace) */
  162. UNIV_INTERN
  163. void
  164. fil_space_clear_crypt_data(
  165. /*=======================*/
  166. byte* page, /*!< in: buffer page */
  167. ulint offset); /*!< in: offset where crypt data is stored */
  168. /*********************************************************************
  169. Parse crypt data log record */
  170. UNIV_INTERN
  171. byte*
  172. fil_parse_write_crypt_data(
  173. /*=======================*/
  174. byte* ptr, /*!< in: start of log record */
  175. byte* end_ptr, /*!< in: end of log record */
  176. buf_block_t*); /*!< in: buffer page to apply record to */
  177. /*********************************************************************
  178. Check if extra buffer shall be allocated for decrypting after read */
  179. UNIV_INTERN
  180. bool
  181. fil_space_check_encryption_read(
  182. /*============================*/
  183. ulint space); /*!< in: tablespace id */
  184. /******************************************************************
  185. Decrypt a page
  186. @return true if page is decrypted, false if not. */
  187. UNIV_INTERN
  188. bool
  189. fil_space_decrypt(
  190. /*==============*/
  191. fil_space_crypt_t* crypt_data, /*!< in: crypt data */
  192. byte* tmp_frame, /*!< in: temporary buffer */
  193. ulint page_size, /*!< in: page size */
  194. byte* src_frame, /*!< in:out: page buffer */
  195. dberr_t* err); /*!< in: out: DB_SUCCESS or
  196. error code */
  197. /*********************************************************************
  198. Encrypt buffer page
  199. @return encrypted page, or original not encrypted page if encrypt
  200. is not needed. */
  201. UNIV_INTERN
  202. byte*
  203. fil_space_encrypt(
  204. /*==============*/
  205. ulint space, /*!< in: tablespace id */
  206. ulint offset, /*!< in: page no */
  207. lsn_t lsn, /*!< in: page lsn */
  208. byte* src_frame, /*!< in: page frame */
  209. ulint size, /*!< in: size of data to encrypt */
  210. byte* dst_frame); /*!< in: where to encrypt to */
  211. /*********************************************************************
  212. Decrypt buffer page
  213. @return decrypted page, or original not encrypted page if decrypt is
  214. not needed.*/
  215. UNIV_INTERN
  216. byte*
  217. fil_space_decrypt(
  218. /*==============*/
  219. ulint space, /*!< in: tablespace id */
  220. byte* src_frame, /*!< in: page frame */
  221. ulint page_size, /*!< in: size of data to encrypt */
  222. byte* dst_frame) /*!< in: where to decrypt to */
  223. __attribute__((warn_unused_result));
  224. /*********************************************************************
  225. fil_space_verify_crypt_checksum
  226. NOTE: currently this function can only be run in single threaded mode
  227. as it modifies srv_checksum_algorithm (temporarily)
  228. @return true if page is encrypted AND OK, false otherwise */
  229. UNIV_INTERN
  230. bool
  231. fil_space_verify_crypt_checksum(
  232. /*============================*/
  233. const byte* src_frame,/*!< in: page frame */
  234. ulint zip_size); /*!< in: size of data to encrypt */
  235. /*********************************************************************
  236. Init threads for key rotation */
  237. UNIV_INTERN
  238. void
  239. fil_crypt_threads_init();
  240. /*********************************************************************
  241. Set thread count (e.g start or stops threads) used for key rotation */
  242. UNIV_INTERN
  243. void
  244. fil_crypt_set_thread_cnt(
  245. /*=====================*/
  246. uint new_cnt); /*!< in: requested #threads */
  247. /*********************************************************************
  248. End threads for key rotation */
  249. UNIV_INTERN
  250. void
  251. fil_crypt_threads_end();
  252. /*********************************************************************
  253. Cleanup resources for threads for key rotation */
  254. UNIV_INTERN
  255. void
  256. fil_crypt_threads_cleanup();
  257. /*********************************************************************
  258. Set rotate key age */
  259. UNIV_INTERN
  260. void
  261. fil_crypt_set_rotate_key_age(
  262. /*=========================*/
  263. uint rotate_age); /*!< in: requested rotate age */
  264. /*********************************************************************
  265. Set rotation threads iops */
  266. UNIV_INTERN
  267. void
  268. fil_crypt_set_rotation_iops(
  269. /*========================*/
  270. uint iops); /*!< in: requested iops */
  271. /*********************************************************************
  272. Mark a space as closing */
  273. UNIV_INTERN
  274. void
  275. fil_space_crypt_mark_space_closing(
  276. /*===============================*/
  277. ulint space); /*!< in: tablespace id */
  278. /*********************************************************************
  279. Wait for crypt threads to stop accessing space */
  280. UNIV_INTERN
  281. void
  282. fil_space_crypt_close_tablespace(
  283. /*=============================*/
  284. ulint space); /*!< in: tablespace id */
  285. /** Struct for retreiving info about encryption */
  286. struct fil_space_crypt_status_t {
  287. ulint space; /*!< tablespace id */
  288. ulint scheme; /*!< encryption scheme */
  289. uint min_key_version; /*!< min key version */
  290. uint current_key_version;/*!< current key version */
  291. uint keyserver_requests;/*!< no of key requests to key server */
  292. ulint key_id; /*!< current key_id */
  293. bool rotating; /*!< is key rotation ongoing */
  294. bool flushing; /*!< is flush at end of rotation ongoing */
  295. ulint rotate_next_page_number; /*!< next page if key rotating */
  296. ulint rotate_max_page_number; /*!< max page if key rotating */
  297. };
  298. /*********************************************************************
  299. Get crypt status for a space
  300. @return 0 if crypt data found */
  301. UNIV_INTERN
  302. int
  303. fil_space_crypt_get_status(
  304. /*=======================*/
  305. ulint id, /*!< in: space id */
  306. struct fil_space_crypt_status_t * status); /*!< out: status */
  307. /** Struct for retreiving statistics about encryption key rotation */
  308. struct fil_crypt_stat_t {
  309. ulint pages_read_from_cache;
  310. ulint pages_read_from_disk;
  311. ulint pages_modified;
  312. ulint pages_flushed;
  313. ulint estimated_iops;
  314. };
  315. /*********************************************************************
  316. Get crypt rotation statistics */
  317. UNIV_INTERN
  318. void
  319. fil_crypt_total_stat(
  320. /*==================*/
  321. fil_crypt_stat_t* stat); /*!< out: crypt stat */
  322. /** Struct for retreiving info about scrubbing */
  323. struct fil_space_scrub_status_t {
  324. ulint space; /*!< tablespace id */
  325. bool compressed; /*!< is space compressed */
  326. time_t last_scrub_completed; /*!< when was last scrub completed */
  327. bool scrubbing; /*!< is scrubbing ongoing */
  328. time_t current_scrub_started; /*!< when started current scrubbing */
  329. ulint current_scrub_active_threads; /*!< current scrub active threads */
  330. ulint current_scrub_page_number; /*!< current scrub page no */
  331. ulint current_scrub_max_page_number; /*!< current scrub max page no */
  332. };
  333. /*********************************************************************
  334. Get scrub status for a space
  335. @return 0 if no scrub info found */
  336. UNIV_INTERN
  337. int
  338. fil_space_get_scrub_status(
  339. /*=======================*/
  340. ulint id, /*!< in: space id */
  341. struct fil_space_scrub_status_t * status); /*!< out: status */
  342. /*********************************************************************
  343. Adjust encrypt tables */
  344. UNIV_INTERN
  345. void
  346. fil_crypt_set_encrypt_tables(
  347. /*=========================*/
  348. uint val); /*!< in: New srv_encrypt_tables setting */
  349. /******************************************************************
  350. Encrypt a buffer */
  351. UNIV_INTERN
  352. byte*
  353. fil_encrypt_buf(
  354. /*============*/
  355. fil_space_crypt_t* crypt_data, /*!< in: crypt data */
  356. ulint space, /*!< in: Space id */
  357. ulint offset, /*!< in: Page offset */
  358. lsn_t lsn, /*!< in: lsn */
  359. byte* src_frame, /*!< in: Source page to be encrypted */
  360. ulint zip_size, /*!< in: compressed size if
  361. row_format compressed */
  362. byte* dst_frame); /*!< in: outbut buffer */
  363. /******************************************************************
  364. Calculate post encryption checksum
  365. @return page checksum or BUF_NO_CHECKSUM_MAGIC
  366. not needed. */
  367. UNIV_INTERN
  368. ulint
  369. fil_crypt_calculate_checksum(
  370. /*=========================*/
  371. ulint zip_size, /*!< in: zip_size or 0 */
  372. byte* dst_frame); /*!< in: page where to calculate */
  373. #ifndef UNIV_NONINL
  374. #include "fil0crypt.ic"
  375. #endif
  376. #endif /* fil0crypt_h */