service_encryption_scheme.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef MYSQL_SERVICE_ENCRYPTION_SCHEME_INCLUDED
  2. /* Copyright (c) 2015, MariaDB
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  13. /**
  14. @file
  15. encryption scheme service
  16. A higher-level access to encryption service.
  17. This is a helper service that storage engines use to encrypt tables on disk.
  18. It requests keys from the plugin, generates temporary or local keys
  19. from the global (as returned by the plugin) keys, etc.
  20. To use the service:
  21. * st_encryption_scheme object is created per space. A "space" can be
  22. a table space in XtraDB/InnoDB, a file in Aria, etc. The whole
  23. space is encrypted with the one key id.
  24. * The service does not take the key and the IV as parameters for
  25. encryption or decryption. Instead it takes two 32-bit integers and
  26. one 64-bit integer (and requests the key from an encryption
  27. plugin, if needed).
  28. * The service requests the global key from the encryption plugin
  29. automatically as needed. Three last keys are cached in the
  30. st_encryption_scheme. Number of key requests (number of cache
  31. misses) are counted in st_encryption_scheme::keyserver_requests
  32. * If an st_encryption_scheme can be used concurrently by different
  33. threads, it needs to be able to lock itself when accessing the key
  34. cache. Set the st_encryption_scheme::locker appropriately. If
  35. non-zero, it will be invoked by encrypt/decrypt functions to lock
  36. and unlock the scheme when needed.
  37. * Implementation details (in particular, key derivation) are defined
  38. by the scheme type. Currently only schema type 1 is supported.
  39. In the schema type 1, every "space" (table space in XtraDB/InnoDB,
  40. file in Aria) is encrypted with a different space-local key:
  41. * Every space has a 16-byte unique identifier (typically it's
  42. generated randomly and stored in the space). The caller should
  43. put it into st_encryption_scheme::iv.
  44. * Space-local key is generated by encrypting this identifier with
  45. the global encryption key (of the given id and version) using AES_ECB.
  46. * Encryption/decryption parameters for a page are typically the
  47. 4-byte space id, 4-byte page position (offset, page number, etc),
  48. and the 8-byte LSN. This guarantees that they'll be different for
  49. any two pages (of the same or different tablespaces) and also that
  50. they'll change for the same page when it's modified. They don't need
  51. to be secret (they create the IV, not the encryption key).
  52. */
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. #define ENCRYPTION_SCHEME_KEY_INVALID -1
  57. #define ENCRYPTION_SCHEME_BLOCK_LENGTH 16
  58. struct st_encryption_scheme_key {
  59. unsigned int version;
  60. unsigned char key[ENCRYPTION_SCHEME_BLOCK_LENGTH];
  61. };
  62. struct st_encryption_scheme {
  63. unsigned char iv[ENCRYPTION_SCHEME_BLOCK_LENGTH];
  64. struct st_encryption_scheme_key key[3];
  65. unsigned int keyserver_requests;
  66. unsigned int key_id;
  67. unsigned int type;
  68. void (*locker)(struct st_encryption_scheme *self, int release);
  69. };
  70. extern struct encryption_scheme_service_st {
  71. int (*encryption_scheme_encrypt_func)
  72. (const unsigned char* src, unsigned int slen,
  73. unsigned char* dst, unsigned int* dlen,
  74. struct st_encryption_scheme *scheme,
  75. unsigned int key_version, unsigned int i32_1,
  76. unsigned int i32_2, unsigned long long i64);
  77. int (*encryption_scheme_decrypt_func)
  78. (const unsigned char* src, unsigned int slen,
  79. unsigned char* dst, unsigned int* dlen,
  80. struct st_encryption_scheme *scheme,
  81. unsigned int key_version, unsigned int i32_1,
  82. unsigned int i32_2, unsigned long long i64);
  83. } *encryption_scheme_service;
  84. #ifdef MYSQL_DYNAMIC_PLUGIN
  85. #define encryption_scheme_encrypt(S,SL,D,DL,SCH,KV,I32,J32,I64) encryption_scheme_service->encryption_scheme_encrypt_func(S,SL,D,DL,SCH,KV,I32,J32,I64)
  86. #define encryption_scheme_decrypt(S,SL,D,DL,SCH,KV,I32,J32,I64) encryption_scheme_service->encryption_scheme_decrypt_func(S,SL,D,DL,SCH,KV,I32,J32,I64)
  87. #else
  88. int encryption_scheme_encrypt(const unsigned char* src, unsigned int slen,
  89. unsigned char* dst, unsigned int* dlen,
  90. struct st_encryption_scheme *scheme,
  91. unsigned int key_version, unsigned int i32_1,
  92. unsigned int i32_2, unsigned long long i64);
  93. int encryption_scheme_decrypt(const unsigned char* src, unsigned int slen,
  94. unsigned char* dst, unsigned int* dlen,
  95. struct st_encryption_scheme *scheme,
  96. unsigned int key_version, unsigned int i32_1,
  97. unsigned int i32_2, unsigned long long i64);
  98. #endif
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #define MYSQL_SERVICE_ENCRYPTION_SCHEME_INCLUDED
  103. #endif