|
1 | | -#ifndef MY_AES_INCLUDED |
2 | | -#define MY_AES_INCLUDED |
3 | | - |
4 | 1 | /* Copyright (c) 2002, 2006 MySQL AB, 2009 Sun Microsystems, Inc. |
5 | 2 | Use is subject to license terms. |
6 | 3 |
|
|
21 | 18 | /* Header file for my_aes.c */ |
22 | 19 | /* Wrapper to give simple interface for MySQL to AES standard encryption */ |
23 | 20 |
|
| 21 | +#ifndef MY_AES_INCLUDED |
| 22 | +#define MY_AES_INCLUDED |
| 23 | + |
| 24 | +/* We expect same result code from encryption functions as in my_aes.h */ |
| 25 | +typedef int Crypt_result; |
| 26 | + |
| 27 | +#define AES_OK 0 |
| 28 | +#define AES_BAD_DATA -1 |
| 29 | +#define AES_BAD_IV -2 |
| 30 | +#define AES_INVALID -3 |
| 31 | +#define AES_OPENSSL_ERROR -4 |
| 32 | +#define AES_BAD_KEYSIZE -5 |
| 33 | +#define AES_KEY_CREATION_FAILED -10 |
| 34 | + |
| 35 | +#define CRYPT_KEY_OK 0 |
| 36 | +#define CRYPT_BUFFER_TO_SMALL -11; |
| 37 | +#define CRYPT_KEY_UNKNOWN -48; |
| 38 | + |
| 39 | +/* The max block sizes of all supported algorithms */ |
| 40 | +#define MY_AES_BLOCK_SIZE 16 |
| 41 | + |
| 42 | +/* The max key length of all supported algorithms */ |
| 43 | +#define MY_AES_MAX_KEY_LENGTH 32 |
| 44 | + |
| 45 | + |
24 | 46 | #include "rijndael.h" |
25 | 47 |
|
26 | 48 | C_MODE_START |
27 | 49 |
|
28 | 50 | #define AES_KEY_LENGTH 128/* Must be 128 192 or 256 */ |
29 | 51 |
|
30 | | -/* |
31 | | - my_aes_encrypt - Crypt buffer with AES encryption algorithm. |
32 | | - source - Pointer to data for encryption |
33 | | - source_length - size of encryption data |
34 | | - dest - buffer to place encrypted data (must be large enough) |
35 | | - key - Key to be used for encryption |
36 | | - kel_length - Length of the key. Will handle keys of any length |
| 52 | +/** |
| 53 | + Crypt buffer with AES dynamic (defined at startup) encryption algorithm. |
| 54 | +
|
| 55 | + SYNOPSIS |
| 56 | + my_aes_encrypt_dynamic() |
| 57 | + @param source [in] Pointer to data for encryption |
| 58 | + @param source_length [in] Size of encryption data |
| 59 | + @param dest [out] Buffer to place encrypted data (must be large enough) |
| 60 | + @param dest_length [out] Pointer to size of encrypted data |
| 61 | + @param key [in] Key to be used for encryption |
| 62 | + @param key_length [in] Length of the key. 16, 24 or 32 |
| 63 | + @param iv [in] Iv to be used for encryption |
| 64 | + @param iv_length [in] Length of the iv. should be 16. |
| 65 | + @param noPadding [in] if set, algorithm specific padding behaviour is used |
| 66 | +
|
| 67 | + Method used defined by calling my_aes_init_dynamic_encrypt() at startup. |
| 68 | +
|
| 69 | + @return |
| 70 | + != 0 error |
| 71 | + 0 no error |
| 72 | +*/ |
| 73 | + |
| 74 | +typedef int (*my_aes_encrypt_dynamic_type)(const uchar* source, uint32 source_length, |
| 75 | + uchar* dest, uint32* dest_length, |
| 76 | + const uchar* key, uint8 key_length, |
| 77 | + const uchar* iv, uint8 iv_length, |
| 78 | + uint noPadding); |
| 79 | + |
| 80 | +extern my_aes_encrypt_dynamic_type my_aes_encrypt_dynamic; |
| 81 | + |
| 82 | +/** |
| 83 | + AES decryption AES dynamic (defined at startup) encryption algorithm. |
| 84 | +
|
| 85 | + SYNOPSIS |
| 86 | + my_aes_decrypt_dynamic() |
| 87 | + @param source [in] Pointer to data to decrypt |
| 88 | + @param source_length [in] Size of data |
| 89 | + @param dest [out] Buffer to place decrypted data (must be large enough) |
| 90 | + @param dest_length [out] Pointer to size of decrypted data |
| 91 | + @param key [in] Key to be used for decryption |
| 92 | + @param key_length [in] Length of the key. 16, 24 or 32 |
| 93 | + @param iv [in] Iv to be used for encryption |
| 94 | + @param iv_length [in] Length of the iv. should be 16. |
| 95 | + @param noPadding [in] if set, algorithm specific padding behaviour is used |
| 96 | +
|
| 97 | + @return |
| 98 | + != 0 error |
| 99 | + 0 no error |
| 100 | +
|
| 101 | + Method used defined by calling my_aes_init_dynamic_encrypt() at startup. |
| 102 | +*/ |
| 103 | + |
| 104 | +typedef int (*my_aes_decrypt_dynamic_type)(const uchar *source, |
| 105 | + uint32 source_length, |
| 106 | + uchar *dest, uint32 *dest_length, |
| 107 | + const uchar *key, uint8 key_length, |
| 108 | + const uchar *iv, uint8 iv_length, |
| 109 | + uint noPadding); |
| 110 | +extern my_aes_decrypt_dynamic_type my_aes_decrypt_dynamic; |
| 111 | + |
| 112 | +/** |
| 113 | + Initialize dynamic crypt functions |
| 114 | +*/ |
| 115 | + |
| 116 | +enum enum_my_aes_encryption_algorithm |
| 117 | +{ |
| 118 | + MY_AES_ALGORITHM_NONE, MY_AES_ALGORITHM_ECB, MY_AES_ALGORITHM_CBC, |
| 119 | + MY_AES_ALGORITHM_CTR |
| 120 | +}; |
37 | 121 |
|
38 | | - returns - size of encrypted data, or negative in case of error. |
| 122 | +my_aes_decrypt_dynamic_type get_aes_decrypt_func(enum enum_my_aes_encryption_algorithm method); |
| 123 | +my_aes_encrypt_dynamic_type get_aes_encrypt_func(enum enum_my_aes_encryption_algorithm method); |
| 124 | + |
| 125 | + |
| 126 | +my_bool my_aes_init_dynamic_encrypt(enum enum_my_aes_encryption_algorithm method); |
| 127 | + |
| 128 | +extern MYSQL_PLUGIN_IMPORT enum enum_my_aes_encryption_algorithm current_aes_dynamic_method; |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +/** |
| 133 | + Calculate key and iv from a given salt and secret as it is handled in openssl |
| 134 | + encrypted files via console |
| 135 | +
|
| 136 | + SYNOPSIS |
| 137 | + my_bytes_to_key() |
| 138 | +
|
| 139 | + @param salt [in] the given salt as extracted from the encrypted file |
| 140 | + @param secret [in] the given secret as String, provided by the user |
| 141 | + @param key [out] 32 Bytes of key are written to this pointer |
| 142 | + @param iv [out] 16 Bytes of iv are written to this pointer |
| 143 | + */ |
| 144 | + |
| 145 | +void my_bytes_to_key(const uchar *salt, |
| 146 | + const char *secret, uchar *key, |
| 147 | + uchar *iv); |
| 148 | + |
| 149 | +/** |
| 150 | + Decode Hexencoded String to uint8[]. |
| 151 | +
|
| 152 | + SYNOPSIS |
| 153 | + my_aes_hex2uint() |
| 154 | + @param iv [in] Pointer to hexadecimal encoded IV String |
| 155 | + @param dest [out] Pointer to output uint8 array. Memory needs to be |
| 156 | + allocated by caller |
| 157 | + @param iv_length [in] Size of destination array. |
| 158 | + */ |
| 159 | + |
| 160 | +void my_aes_hex2uint(const char *in, uchar *out, int dest_length); |
| 161 | + |
| 162 | +/** |
| 163 | + Crypt buffer with AES encryption algorithm. |
| 164 | +
|
| 165 | + SYNOPSIS |
| 166 | + my_aes_encrypt() |
| 167 | +
|
| 168 | + @param source Pointer to data for encryption |
| 169 | + @param source_length Size of encryption data |
| 170 | + @param dest Buffer to place encrypted data (must be large enough) |
| 171 | + @param key Key to be used for encryption |
| 172 | + @param kel_length Length of the key. Will handle keys of any length |
| 173 | +
|
| 174 | + @return Size of encrypted data, or negative in case of error. |
39 | 175 | */ |
40 | 176 |
|
41 | | -int my_aes_encrypt(const char *source, int source_length, char *dest, |
| 177 | +int my_aes_encrypt(const uchar *source, int source_length, uchar *dest, |
42 | 178 | const char *key, int key_length); |
43 | 179 |
|
44 | | -/* |
45 | | - my_aes_decrypt - DeCrypt buffer with AES encryption algorithm. |
46 | | - source - Pointer to data for decryption |
47 | | - source_length - size of encrypted data |
48 | | - dest - buffer to place decrypted data (must be large enough) |
49 | | - key - Key to be used for decryption |
50 | | - kel_length - Length of the key. Will handle keys of any length |
| 180 | +/** |
| 181 | + DeCrypt buffer with AES encryption algorithm. |
| 182 | +
|
| 183 | + SYNOPSIS |
| 184 | + my_aes_decrypt() |
51 | 185 |
|
52 | | - returns - size of original data, or negative in case of error. |
| 186 | + @param source Pointer to data for decryption |
| 187 | + @param source_length size of encrypted data |
| 188 | + @param dest buffer to place decrypted data (must be large enough) |
| 189 | + @param key Key to be used for decryption |
| 190 | + @param kel_length Length of the key. Will handle keys of any length |
| 191 | +
|
| 192 | + @return size of original data, or negative in case of error. |
53 | 193 | */ |
54 | 194 |
|
55 | 195 |
|
56 | | -int my_aes_decrypt(const char *source, int source_length, char *dest, |
| 196 | +int my_aes_decrypt(const uchar *source, int source_length, uchar *dest, |
57 | 197 | const char *key, int key_length); |
58 | 198 |
|
59 | | -/* |
60 | | - my_aes_get_size - get size of buffer which will be large enough for encrypted |
61 | | - data |
62 | | - source_length - length of data to be encrypted |
| 199 | +/** |
| 200 | + get size of buffer which will be large enough for encrypted data |
| 201 | +
|
| 202 | + SYNOPSIS |
| 203 | + my_aes_get_size() |
| 204 | + @param source_length Length of data to be encrypted |
63 | 205 |
|
64 | | - returns - size of buffer required to store encrypted data |
| 206 | + @return Size of buffer required to store encrypted data |
65 | 207 | */ |
66 | 208 |
|
67 | 209 | int my_aes_get_size(int source_length); |
|
0 commit comments