my_crypt.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright (c) 2014 Google Inc.
  3. Copyright (c) 2014, 2015 MariaDB Corporation
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  14. #ifndef MY_CRYPT_INCLUDED
  15. #define MY_CRYPT_INCLUDED
  16. #include <my_global.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* return values from my_aes_encrypt/my_aes_decrypt functions */
  21. #define MY_AES_OK 0
  22. #define MY_AES_BAD_DATA -100
  23. #define MY_AES_OPENSSL_ERROR -101
  24. #define MY_AES_BAD_KEYSIZE -102
  25. /* The block size for all supported algorithms */
  26. #define MY_AES_BLOCK_SIZE 16
  27. /* The max key length of all supported algorithms */
  28. #define MY_AES_MAX_KEY_LENGTH 32
  29. #define MY_AES_CTX_SIZE 512
  30. enum my_aes_mode {
  31. MY_AES_ECB, MY_AES_CBC
  32. #ifdef HAVE_EncryptAes128Ctr
  33. , MY_AES_CTR
  34. #endif
  35. #ifdef HAVE_EncryptAes128Gcm
  36. , MY_AES_GCM
  37. #endif
  38. };
  39. int my_aes_crypt_init(void *ctx, enum my_aes_mode mode, int flags,
  40. const unsigned char* key, unsigned int klen,
  41. const unsigned char* iv, unsigned int ivlen);
  42. int my_aes_crypt_update(void *ctx, const uchar *src, uint slen,
  43. uchar *dst, uint *dlen);
  44. int my_aes_crypt_finish(void *ctx, uchar *dst, uint *dlen);
  45. int my_aes_crypt(enum my_aes_mode mode, int flags,
  46. const uchar *src, uint slen, uchar *dst, uint *dlen,
  47. const uchar *key, uint klen, const uchar *iv, uint ivlen);
  48. /*
  49. calculate the length of the cyphertext from the length of the plaintext
  50. for different AES encryption modes with padding enabled.
  51. Without padding (ENCRYPTION_FLAG_NOPAD) cyphertext has the same length
  52. as the plaintext
  53. */
  54. static inline uint my_aes_get_size(enum my_aes_mode mode __attribute__((unused)), uint source_length)
  55. {
  56. #ifdef HAVE_EncryptAes128Ctr
  57. if (mode == MY_AES_CTR)
  58. return source_length;
  59. #ifdef HAVE_EncryptAes128Gcm
  60. if (mode == MY_AES_GCM)
  61. return source_length + MY_AES_BLOCK_SIZE;
  62. #endif
  63. #endif
  64. return (source_length / MY_AES_BLOCK_SIZE + 1) * MY_AES_BLOCK_SIZE;
  65. }
  66. static inline uint my_aes_ctx_size(enum my_aes_mode mode __attribute__((unused)))
  67. {
  68. return MY_AES_CTX_SIZE;
  69. }
  70. int my_random_bytes(uchar* buf, int num);
  71. #ifdef __cplusplus
  72. }
  73. #endif
  74. #endif /* MY_CRYPT_INCLUDED */