| 1 | #ifndef MYSQL_SERVICE_MY_CRYPT_INCLUDED |
| 2 | #define MYSQL_SERVICE_MY_CRYPT_INCLUDED |
| 3 | |
| 4 | /* |
| 5 | Copyright (c) 2014 Google Inc. |
| 6 | Copyright (c) 2014, 2015 MariaDB Corporation |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; version 2 of the License. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 20 | |
| 21 | /** |
| 22 | @file |
| 23 | my crypt service |
| 24 | |
| 25 | AES encryption functions, and a function to generate random bytes. |
| 26 | |
| 27 | Include my_config.h before this file to use CTR and GCM modes |
| 28 | (they only work if server was compiled with openssl). |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | /* return values from my_aes_encrypt/my_aes_decrypt functions */ |
| 37 | #define MY_AES_OK 0 |
| 38 | #define MY_AES_BAD_DATA -100 |
| 39 | #define MY_AES_OPENSSL_ERROR -101 |
| 40 | #define MY_AES_BAD_KEYSIZE -102 |
| 41 | |
| 42 | /* The block size for all supported algorithms */ |
| 43 | #define MY_AES_BLOCK_SIZE 16 |
| 44 | |
| 45 | /* The max key length of all supported algorithms */ |
| 46 | #define MY_AES_MAX_KEY_LENGTH 32 |
| 47 | |
| 48 | #define MY_AES_CTX_SIZE 512 |
| 49 | |
| 50 | enum my_aes_mode { |
| 51 | MY_AES_ECB, MY_AES_CBC |
| 52 | #ifdef HAVE_EncryptAes128Ctr |
| 53 | , MY_AES_CTR |
| 54 | #endif |
| 55 | #ifdef HAVE_EncryptAes128Gcm |
| 56 | , MY_AES_GCM |
| 57 | #endif |
| 58 | }; |
| 59 | |
| 60 | extern struct my_crypt_service_st { |
| 61 | int (*my_aes_crypt_init)(void *ctx, enum my_aes_mode mode, int flags, |
| 62 | const unsigned char* key, unsigned int klen, |
| 63 | const unsigned char* iv, unsigned int ivlen); |
| 64 | int (*my_aes_crypt_update)(void *ctx, const unsigned char *src, unsigned int slen, |
| 65 | unsigned char *dst, unsigned int *dlen); |
| 66 | int (*my_aes_crypt_finish)(void *ctx, unsigned char *dst, unsigned int *dlen); |
| 67 | int (*my_aes_crypt)(enum my_aes_mode mode, int flags, |
| 68 | const unsigned char *src, unsigned int slen, unsigned char *dst, unsigned int *dlen, |
| 69 | const unsigned char *key, unsigned int klen, const unsigned char *iv, unsigned int ivlen); |
| 70 | unsigned int (*my_aes_get_size)(enum my_aes_mode mode, unsigned int source_length); |
| 71 | unsigned int (*my_aes_ctx_size)(enum my_aes_mode mode); |
| 72 | int (*my_random_bytes)(unsigned char* buf, int num); |
| 73 | } *my_crypt_service; |
| 74 | |
| 75 | #ifdef MYSQL_DYNAMIC_PLUGIN |
| 76 | |
| 77 | #define my_aes_crypt_init(A,B,C,D,E,F,G) \ |
| 78 | my_crypt_service->my_aes_crypt_init(A,B,C,D,E,F,G) |
| 79 | |
| 80 | #define my_aes_crypt_update(A,B,C,D,E) \ |
| 81 | my_crypt_service->my_aes_crypt_update(A,B,C,D,E) |
| 82 | |
| 83 | #define my_aes_crypt_finish(A,B,C) \ |
| 84 | my_crypt_service->my_aes_crypt_finish(A,B,C) |
| 85 | |
| 86 | #define my_aes_crypt(A,B,C,D,E,F,G,H,I,J) \ |
| 87 | my_crypt_service->my_aes_crypt(A,B,C,D,E,F,G,H,I,J) |
| 88 | |
| 89 | #define my_aes_get_size(A,B)\ |
| 90 | my_crypt_service->my_aes_get_size(A,B) |
| 91 | |
| 92 | #define my_aes_ctx_size(A)\ |
| 93 | my_crypt_service->my_aes_ctx_size(A) |
| 94 | |
| 95 | #define my_random_bytes(A,B)\ |
| 96 | my_crypt_service->my_random_bytes(A,B) |
| 97 | |
| 98 | #else |
| 99 | |
| 100 | int my_aes_crypt_init(void *ctx, enum my_aes_mode mode, int flags, |
| 101 | const unsigned char* key, unsigned int klen, |
| 102 | const unsigned char* iv, unsigned int ivlen); |
| 103 | int my_aes_crypt_update(void *ctx, const unsigned char *src, unsigned int slen, |
| 104 | unsigned char *dst, unsigned int *dlen); |
| 105 | int my_aes_crypt_finish(void *ctx, unsigned char *dst, unsigned int *dlen); |
| 106 | int my_aes_crypt(enum my_aes_mode mode, int flags, |
| 107 | const unsigned char *src, unsigned int slen, unsigned char *dst, unsigned int *dlen, |
| 108 | const unsigned char *key, unsigned int klen, const unsigned char *iv, unsigned int ivlen); |
| 109 | |
| 110 | int my_random_bytes(unsigned char* buf, int num); |
| 111 | unsigned int my_aes_get_size(enum my_aes_mode mode, unsigned int source_length); |
| 112 | unsigned int my_aes_ctx_size(enum my_aes_mode mode); |
| 113 | #endif |
| 114 | |
| 115 | |
| 116 | #ifdef __cplusplus |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | #endif /* MYSQL_SERVICE_MY_CRYPT_INCLUDED */ |
| 121 | |