Debian Codegraph @

components/keyrings/common/encryption/aes.h

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 #ifndef AES_INCLUDED
25 #define AES_INCLUDED
26
27 #include <functional>
28 #include <map>
29 #include <string>
30 #include <utility>
31
32 #include <openssl/evp.h>
33
34 namespace keyring_common {
35 namespace aes_encryption {
36
37 /** Supported AES cipher/block mode combos */
38 enum class Keyring_aes_opmode {
39 keyring_aes_256_ecb = 0,
40 keyring_aes_256_cbc,
41 keyring_aes_256_cfb1,
42 keyring_aes_256_cfb8,
43 keyring_aes_256_cfb128,
44 keyring_aes_256_ofb,
45 /* Add new values above this */
46 keyring_aes_opmode_invalid
47 };
48
49 enum aes_return_status {
50 AES_OP_OK,
51 AES_OUTPUT_SIZE_NULL,
52 AES_KEY_TRANSFORMATION_ERROR,
53 AES_CTX_ALLOCATION_ERROR,
54 AES_INVALID_BLOCK_MODE,
55 AES_IV_EMPTY,
56 AES_ENCRYPTION_ERROR,
57 AES_DECRYPTION_ERROR
58 };
59
60 using block_mode_key = std::pair<std::string, size_t>;
61 using Known_block_mode_map = std::map<block_mode_key, Keyring_aes_opmode>;
62
63 class Aes_operation_context final {
64 public:
65 Aes_operation_context(const std::string data_id, const std::string auth_id,
66 const std::string mode, size_t block_size);
67 ~Aes_operation_context() = default;
68 const std::string data_id() const { return data_id_; }
69 const std::string auth_id() const { return auth_id_; }
70 Keyring_aes_opmode opmode() const { return opmode_; }
71 bool valid() const { return valid_; }
72 static const Known_block_mode_map s_blockmodes;
73
74 private:
75 std::string data_id_;
76 std::string auth_id_;
77 Keyring_aes_opmode opmode_;
78 bool valid_;
79 };
80
81 const EVP_CIPHER *aes_evp_type(const Keyring_aes_opmode mode);
82
83 size_t get_ciphertext_size(size_t input_size, const Keyring_aes_opmode mode);
84
85 aes_return_status aes_encrypt(const unsigned char *source,
86 unsigned int source_length, unsigned char *dest,
87 const unsigned char *key, unsigned int key_length,
88 Keyring_aes_opmode mode, const unsigned char *iv,
89 bool padding, size_t *encrypted_length);
90
91 aes_return_status aes_decrypt(const unsigned char *source,
92 unsigned int source_length, unsigned char *dest,
93 const unsigned char *key, unsigned int key_length,
94 Keyring_aes_opmode mode, const unsigned char *iv,
95 bool padding, size_t *decrypted_length);
96
97 } // namespace aes_encryption
98 } // namespace keyring_common
99
100 #endif // !AES_INCLUDED