service_encryption.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef MYSQL_SERVICE_ENCRYPTION_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 service
  16. Functions to support data encryption and encryption key management.
  17. They are normally implemented in an encryption plugin, so this service
  18. connects encryption *consumers* (e.g. storage engines) to the encryption
  19. *provider* (encryption plugin).
  20. */
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #ifndef MYSQL_ABI_CHECK
  25. #ifdef _WIN32
  26. #include <malloc.h>
  27. #ifndef __cplusplus
  28. #define inline __inline
  29. #endif
  30. #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
  31. #include <stdlib.h>
  32. #else
  33. #include <alloca.h>
  34. #endif
  35. #endif
  36. /* returned from encryption_key_get_latest_version() */
  37. #define ENCRYPTION_KEY_VERSION_INVALID (~(unsigned int)0)
  38. #define ENCRYPTION_KEY_NOT_ENCRYPTED (0)
  39. #define ENCRYPTION_KEY_SYSTEM_DATA 1
  40. #define ENCRYPTION_KEY_TEMPORARY_DATA 2
  41. /* returned from encryption_key_get() */
  42. #define ENCRYPTION_KEY_BUFFER_TOO_SMALL (100)
  43. #define ENCRYPTION_FLAG_DECRYPT 0
  44. #define ENCRYPTION_FLAG_ENCRYPT 1
  45. #define ENCRYPTION_FLAG_NOPAD 2
  46. struct encryption_service_st {
  47. unsigned int (*encryption_key_get_latest_version_func)(unsigned int key_id);
  48. unsigned int (*encryption_key_get_func)(unsigned int key_id, unsigned int key_version,
  49. unsigned char* buffer, unsigned int* length);
  50. unsigned int (*encryption_ctx_size_func)(unsigned int key_id, unsigned int key_version);
  51. int (*encryption_ctx_init_func)(void *ctx, const unsigned char* key, unsigned int klen,
  52. const unsigned char* iv, unsigned int ivlen,
  53. int flags, unsigned int key_id,
  54. unsigned int key_version);
  55. int (*encryption_ctx_update_func)(void *ctx, const unsigned char* src, unsigned int slen,
  56. unsigned char* dst, unsigned int* dlen);
  57. int (*encryption_ctx_finish_func)(void *ctx, unsigned char* dst, unsigned int* dlen);
  58. unsigned int (*encryption_encrypted_length_func)(unsigned int slen, unsigned int key_id, unsigned int key_version);
  59. };
  60. #ifdef MYSQL_DYNAMIC_PLUGIN
  61. extern struct encryption_service_st *encryption_service;
  62. #define encryption_key_get_latest_version(KI) encryption_service->encryption_key_get_latest_version_func(KI)
  63. #define encryption_key_get(KI,KV,K,S) encryption_service->encryption_key_get_func((KI),(KV),(K),(S))
  64. #define encryption_ctx_size(KI,KV) encryption_service->encryption_ctx_size_func((KI),(KV))
  65. #define encryption_ctx_init(CTX,K,KL,IV,IVL,F,KI,KV) encryption_service->encryption_ctx_init_func((CTX),(K),(KL),(IV),(IVL),(F),(KI),(KV))
  66. #define encryption_ctx_update(CTX,S,SL,D,DL) encryption_service->encryption_ctx_update_func((CTX),(S),(SL),(D),(DL))
  67. #define encryption_ctx_finish(CTX,D,DL) encryption_service->encryption_ctx_finish_func((CTX),(D),(DL))
  68. #define encryption_encrypted_length(SL,KI,KV) encryption_service->encryption_encrypted_length_func((SL),(KI),(KV))
  69. #else
  70. extern struct encryption_service_st encryption_handler;
  71. #define encryption_key_get_latest_version(KI) encryption_handler.encryption_key_get_latest_version_func(KI)
  72. #define encryption_key_get(KI,KV,K,S) encryption_handler.encryption_key_get_func((KI),(KV),(K),(S))
  73. #define encryption_ctx_size(KI,KV) encryption_handler.encryption_ctx_size_func((KI),(KV))
  74. #define encryption_ctx_init(CTX,K,KL,IV,IVL,F,KI,KV) encryption_handler.encryption_ctx_init_func((CTX),(K),(KL),(IV),(IVL),(F),(KI),(KV))
  75. #define encryption_ctx_update(CTX,S,SL,D,DL) encryption_handler.encryption_ctx_update_func((CTX),(S),(SL),(D),(DL))
  76. #define encryption_ctx_finish(CTX,D,DL) encryption_handler.encryption_ctx_finish_func((CTX),(D),(DL))
  77. #define encryption_encrypted_length(SL,KI,KV) encryption_handler.encryption_encrypted_length_func((SL),(KI),(KV))
  78. #endif
  79. static inline unsigned int encryption_key_id_exists(unsigned int id)
  80. {
  81. return encryption_key_get_latest_version(id) != ENCRYPTION_KEY_VERSION_INVALID;
  82. }
  83. static inline unsigned int encryption_key_version_exists(unsigned int id, unsigned int version)
  84. {
  85. unsigned int unused;
  86. return encryption_key_get(id, version, NULL, &unused) != ENCRYPTION_KEY_VERSION_INVALID;
  87. }
  88. static inline int encryption_crypt(const unsigned char* src, unsigned int slen,
  89. unsigned char* dst, unsigned int* dlen,
  90. const unsigned char* key, unsigned int klen,
  91. const unsigned char* iv, unsigned int ivlen,
  92. int flags, unsigned int key_id, unsigned int key_version)
  93. {
  94. void *ctx= alloca(encryption_ctx_size(key_id, key_version));
  95. int res1, res2;
  96. unsigned int d1, d2;
  97. if ((res1= encryption_ctx_init(ctx, key, klen, iv, ivlen, flags, key_id, key_version)))
  98. return res1;
  99. res1= encryption_ctx_update(ctx, src, slen, dst, &d1);
  100. res2= encryption_ctx_finish(ctx, dst + d1, &d2);
  101. *dlen= d1 + d2;
  102. return res1 ? res1 : res2;
  103. }
  104. #ifdef __cplusplus
  105. }
  106. #endif
  107. #define MYSQL_SERVICE_ENCRYPTION_INCLUDED
  108. #endif