Debian Codegraph @

components/keyrings/common/encryption/aes.cc

raw
1 /* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <algorithm>
25 #include <locale>
26 #include <memory>
27
28 #include <include/scope_guard.h>
29
30 #include <openssl/aes.h>
31 #include <openssl/bio.h>
32 #include <openssl/err.h>
33
34 #include <openssl/sha.h>
35
36 #include "aes.h"
37
38 namespace keyring_common {
39 namespace aes_encryption {
40
41 const Known_block_mode_map Aes_operation_context::s_blockmodes = {
42 {std::make_pair("ecb", 256), Keyring_aes_opmode::keyring_aes_256_ecb},
43 {std::make_pair("cbc", 256), Keyring_aes_opmode::keyring_aes_256_cbc},
44 {std::make_pair("cfb1", 256), Keyring_aes_opmode::keyring_aes_256_cfb1},
45 {std::make_pair("cfb8", 256), Keyring_aes_opmode::keyring_aes_256_cfb8},
46 {std::make_pair("cfb128", 256), Keyring_aes_opmode::keyring_aes_256_cfb128},
47 {std::make_pair("ofb", 256), Keyring_aes_opmode::keyring_aes_256_ofb}};
48
49 Aes_operation_context::Aes_operation_context(const std::string data_id,
50 const std::string auth_id,
51 const std::string mode,
52 size_t block_size)
53 : data_id_(data_id),
54 auth_id_(auth_id),
55 opmode_(Keyring_aes_opmode::keyring_aes_opmode_invalid),
56 valid_(false) {
57 block_mode_key key(mode, block_size);
58 auto it = Aes_operation_context::s_blockmodes.find(key);
59 if (it != Aes_operation_context::s_blockmodes.end()) {
60 opmode_ = it->second;
61 }
62
63 valid_ = (opmode_ != Keyring_aes_opmode::keyring_aes_opmode_invalid);
64 }
65
66 /* keep in sync with enum Keyring_aes_opmode in keyring_encryption_service.h */
67 size_t aes_opmode_key_sizes[] = {
68 256 /* aes-256-ecb */, 256 /* aes-256-cbc */, 256 /* aes-256-cfb1 */,
69 256 /* aes-256-cfb8 */, 256 /* aes-256-cfb128 */, 256 /* aes-256-ofb */
70 };
71
72 const EVP_CIPHER *aes_evp_type(const Keyring_aes_opmode mode) {
73 switch (mode) {
74 case Keyring_aes_opmode::keyring_aes_256_ecb:
75 return EVP_aes_256_ecb();
76 case Keyring_aes_opmode::keyring_aes_256_cbc:
77 return EVP_aes_256_cbc();
78 case Keyring_aes_opmode::keyring_aes_256_cfb1:
79 return EVP_aes_256_cfb1();
80 case Keyring_aes_opmode::keyring_aes_256_cfb8:
81 return EVP_aes_256_cfb8();
82 case Keyring_aes_opmode::keyring_aes_256_cfb128:
83 return EVP_aes_256_cfb128();
84 case Keyring_aes_opmode::keyring_aes_256_ofb:
85 return EVP_aes_256_ofb();
86 case Keyring_aes_opmode::keyring_aes_opmode_invalid:
87 [[fallthrough]];
88 default:
89 return nullptr;
90 }
91 }
92
93 /**
94 Transforms an arbitrary long key into a fixed length AES key
95
96 @param [in] key Key to use for real key creation
97 @param [in] key_length Length of the key
98 @param [out] rkey Generated key
99 @param [out] rkey_size Size of generated key
100 @param [in] opmode encryption mode
101
102 @returns Key generation status
103 @retval true Success
104 @retval false Error
105 */
106
107 bool aes_create_key(const unsigned char *key, unsigned int key_length,
108 std::unique_ptr<unsigned char[]> &rkey, size_t *rkey_size,
109 Keyring_aes_opmode opmode) {
110 if (rkey_size == nullptr) return false;
111 *rkey_size = aes_opmode_key_sizes[static_cast<unsigned int>(opmode)] / 8;
112 rkey = std::make_unique<unsigned char[]>(*rkey_size);
113 if (rkey.get() == nullptr) return false;
114 switch (*rkey_size) {
115 case 32: /* 256 bit key */ {
116 EVP_MD_CTX *md_ctx = EVP_MD_CTX_create();
117 EVP_DigestInit_ex(md_ctx, EVP_sha256(), nullptr);
118 EVP_DigestUpdate(
119 md_ctx, reinterpret_cast<void *>(const_cast<unsigned char *>(key)),
120 (size_t)key_length);
121 EVP_DigestFinal_ex(md_ctx, rkey.get(), nullptr);
122 EVP_MD_CTX_destroy(md_ctx);
123 break;
124 }
125 default:
126 return false;
127 }
128 return true;
129 }
130
131 size_t get_ciphertext_size(size_t input_size, const Keyring_aes_opmode mode) {
132 const EVP_CIPHER *cipher = aes_evp_type(mode);
133 size_t block_size;
134
135 block_size = EVP_CIPHER_block_size(cipher);
136
137 return block_size > 1 ? block_size * (input_size / block_size) + block_size
138 : input_size;
139 }
140
141 aes_return_status aes_encrypt(const unsigned char *source,
142 unsigned int source_length, unsigned char *dest,
143 const unsigned char *key, unsigned int key_length,
144 Keyring_aes_opmode mode, const unsigned char *iv,
145 bool padding, size_t *encrypted_length) {
146 if (encrypted_length == nullptr) return AES_OUTPUT_SIZE_NULL;
147
148 #if OPENSSL_VERSION_NUMBER < 0x10100000L
149 EVP_CIPHER_CTX stack_ctx;
150 EVP_CIPHER_CTX *ctx = &stack_ctx;
151 #else /* OPENSSL_VERSION_NUMBER < 0x10100000L */
152 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
153 if (ctx == nullptr) return AES_CTX_ALLOCATION_ERROR;
154 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
155
156 auto cleanup_guard = create_scope_guard([&] {
157 /* need to explicitly clean up the error if we want to ignore it */
158 ERR_clear_error();
159 #if OPENSSL_VERSION_NUMBER < 0x10100000L
160 EVP_CIPHER_CTX_cleanup(ctx);
161 #else /* OPENSSL_VERSION_NUMBER < 0x10100000L */
162 EVP_CIPHER_CTX_free(ctx);
163 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
164 });
165
166 const EVP_CIPHER *cipher = aes_evp_type(mode);
167 if (cipher == nullptr) return AES_INVALID_BLOCK_MODE;
168
169 /* The real key to be used for encryption */
170 std::unique_ptr<unsigned char[]> rkey;
171 size_t rkey_size;
172 if (aes_create_key(key, key_length, rkey, &rkey_size, mode) == false)
173 return AES_KEY_TRANSFORMATION_ERROR;
174
175 if (EVP_CIPHER_iv_length(cipher) > 0 && !iv) return AES_IV_EMPTY;
176
177 int u_len, f_len;
178
179 if (!EVP_EncryptInit(ctx, cipher, rkey.get(), iv))
180 return AES_ENCRYPTION_ERROR;
181 if (!EVP_CIPHER_CTX_set_padding(ctx, padding)) return AES_ENCRYPTION_ERROR;
182 if (!EVP_EncryptUpdate(ctx, dest, &u_len, source, source_length))
183 return AES_ENCRYPTION_ERROR;
184 if (!EVP_EncryptFinal(ctx, dest + u_len, &f_len)) return AES_ENCRYPTION_ERROR;
185
186 /* All is well */
187 *encrypted_length = static_cast<size_t>(u_len + f_len);
188 return AES_OP_OK;
189 }
190
191 aes_return_status aes_decrypt(const unsigned char *source,
192 unsigned int source_length, unsigned char *dest,
193 const unsigned char *key, unsigned int key_length,
194 enum Keyring_aes_opmode mode,
195 const unsigned char *iv, bool padding,
196 size_t *decrypted_length) {
197 if (decrypted_length == nullptr) return AES_OUTPUT_SIZE_NULL;
198
199 #if OPENSSL_VERSION_NUMBER < 0x10100000L
200 EVP_CIPHER_CTX stack_ctx;
201 EVP_CIPHER_CTX *ctx = &stack_ctx;
202 #else /* OPENSSL_VERSION_NUMBER < 0x10100000L */
203 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
204 if (ctx == nullptr) return AES_CTX_ALLOCATION_ERROR;
205 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
206
207 auto cleanup_guard = create_scope_guard([&] {
208 /* need to explicitly clean up the error if we want to ignore it */
209 ERR_clear_error();
210 #if OPENSSL_VERSION_NUMBER < 0x10100000L
211 EVP_CIPHER_CTX_cleanup(ctx);
212 #else /* OPENSSL_VERSION_NUMBER < 0x10100000L */
213 EVP_CIPHER_CTX_free(ctx);
214 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
215 });
216
217 const EVP_CIPHER *cipher = aes_evp_type(mode);
218 if (cipher == nullptr) return AES_INVALID_BLOCK_MODE;
219
220 /* The real key to be used for encryption */
221 std::unique_ptr<unsigned char[]> rkey;
222 size_t rkey_size;
223 if (aes_create_key(key, key_length, rkey, &rkey_size, mode) == false)
224 return AES_KEY_TRANSFORMATION_ERROR;
225
226 if (EVP_CIPHER_iv_length(cipher) > 0 && !iv) return AES_IV_EMPTY;
227
228 int u_len, f_len;
229
230 if (!EVP_DecryptInit(ctx, aes_evp_type(mode), rkey.get(), iv))
231 return AES_DECRYPTION_ERROR;
232 if (!EVP_CIPHER_CTX_set_padding(ctx, padding)) return AES_DECRYPTION_ERROR;
233 if (!EVP_DecryptUpdate(ctx, dest, &u_len, source, source_length))
234 return AES_DECRYPTION_ERROR;
235 if (!EVP_DecryptFinal_ex(ctx, dest + u_len, &f_len))
236 return AES_DECRYPTION_ERROR;
237
238 /* All is well */
239 *decrypted_length = static_cast<size_t>(u_len + f_len);
240 return AES_OP_OK;
241 }
242
243 } // namespace aes_encryption
244 } // namespace keyring_common