| 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 <cstdio> /* std::remove */ |
| 25 | #include <memory> |
| 26 | #include <string> |
| 27 | |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | #include "components/keyrings/common/data_file/reader.h" |
| 31 | #include "components/keyrings/common/data_file/writer.h" |
| 32 | #include "components/keyrings/common/encryption/aes.h" |
| 33 | |
| 34 | namespace aes_encryption_unittest { |
| 35 | |
| 36 | using keyring_common::aes_encryption::aes_decrypt; |
| 37 | using keyring_common::aes_encryption::aes_encrypt; |
| 38 | using keyring_common::aes_encryption::aes_return_status; |
| 39 | using keyring_common::aes_encryption::get_ciphertext_size; |
| 40 | using keyring_common::aes_encryption::Keyring_aes_opmode; |
| 41 | using keyring_common::data_file::File_reader; |
| 42 | using keyring_common::data_file::File_writer; |
| 43 | |
| 44 | class AESEncryption_test : public ::testing::Test {}; |
| 45 | |
| 46 | TEST_F(AESEncryption_test, CiphertextsizeTest) { |
| 47 | size_t input_size = 1024; |
| 48 | size_t output_size = |
| 49 | get_ciphertext_size(input_size, Keyring_aes_opmode::keyring_aes_256_cbc); |
| 50 | EXPECT_TRUE(output_size > input_size); |
| 51 | |
| 52 | output_size = |
| 53 | get_ciphertext_size(input_size, Keyring_aes_opmode::keyring_aes_256_ecb); |
| 54 | EXPECT_TRUE(output_size > input_size); |
| 55 | |
| 56 | output_size = |
| 57 | get_ciphertext_size(input_size, Keyring_aes_opmode::keyring_aes_256_cbc); |
| 58 | EXPECT_TRUE(output_size > input_size); |
| 59 | |
| 60 | output_size = |
| 61 | get_ciphertext_size(input_size, Keyring_aes_opmode::keyring_aes_256_cfb1); |
| 62 | EXPECT_TRUE(output_size == input_size); |
| 63 | |
| 64 | output_size = |
| 65 | get_ciphertext_size(input_size, Keyring_aes_opmode::keyring_aes_256_cfb8); |
| 66 | EXPECT_TRUE(output_size == input_size); |
| 67 | |
| 68 | output_size = get_ciphertext_size(input_size, |
| 69 | Keyring_aes_opmode::keyring_aes_256_cfb128); |
| 70 | EXPECT_TRUE(output_size == input_size); |
| 71 | |
| 72 | output_size = |
| 73 | get_ciphertext_size(input_size, Keyring_aes_opmode::keyring_aes_256_ofb); |
| 74 | EXPECT_TRUE(output_size == input_size); |
| 75 | } |
| 76 | |
| 77 | TEST_F(AESEncryption_test, EncryptDecryptTest) { |
| 78 | std::string source_1("Quick brown fox jumped over the lazy dog"); |
| 79 | std::string pass1("Ac32=x133/#@$R"); |
| 80 | std::string iv1("abcdefgh12345678"); |
| 81 | std::unique_ptr<unsigned char[]> output_1, output_1_1; |
| 82 | size_t output_1_size, output_1_1_size; |
| 83 | output_1 = std::make_unique<unsigned char[]>(get_ciphertext_size( |
| 84 | source_1.length(), Keyring_aes_opmode::keyring_aes_256_cbc)); |
| 85 | EXPECT_TRUE(output_1.get() != nullptr); |
| 86 | aes_return_status source_1_encryption_status = |
| 87 | aes_encrypt(reinterpret_cast<const unsigned char *>(source_1.c_str()), |
| 88 | static_cast<unsigned int>(source_1.length()), output_1.get(), |
| 89 | reinterpret_cast<const unsigned char *>(pass1.c_str()), |
| 90 | static_cast<unsigned int>(pass1.length()), |
| 91 | Keyring_aes_opmode::keyring_aes_256_cbc, |
| 92 | reinterpret_cast<const unsigned char *>(iv1.c_str()), true, |
| 93 | &output_1_size); |
| 94 | EXPECT_TRUE(source_1_encryption_status == aes_return_status::AES_OP_OK); |
| 95 | |
| 96 | output_1_1 = std::make_unique<unsigned char[]>(get_ciphertext_size( |
| 97 | source_1.length(), Keyring_aes_opmode::keyring_aes_256_cbc)); |
| 98 | EXPECT_TRUE(output_1_1.get() != nullptr); |
| 99 | aes_return_status source_1_decryption_status = aes_decrypt( |
| 100 | output_1.get(), |
| 101 | static_cast<unsigned int>(get_ciphertext_size( |
| 102 | source_1.length(), Keyring_aes_opmode::keyring_aes_256_cbc)), |
| 103 | output_1_1.get(), reinterpret_cast<const unsigned char *>(pass1.c_str()), |
| 104 | static_cast<unsigned int>(pass1.length()), |
| 105 | Keyring_aes_opmode::keyring_aes_256_cbc, |
| 106 | reinterpret_cast<const unsigned char *>(iv1.c_str()), true, |
| 107 | &output_1_1_size); |
| 108 | EXPECT_TRUE(source_1_decryption_status == aes_return_status::AES_OP_OK); |
| 109 | |
| 110 | std::string decrypted_source_1(reinterpret_cast<char *>(output_1_1.get()), |
| 111 | output_1_1_size); |
| 112 | |
| 113 | EXPECT_TRUE(decrypted_source_1 == source_1); |
| 114 | |
| 115 | std::string source_2( |
| 116 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " |
| 117 | "eiusmod " |
| 118 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad " |
| 119 | "minim " |
| 120 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip " |
| 121 | "ex ea " |
| 122 | "commodo consequat. Duis aute irure dolor in reprehenderit in " |
| 123 | "voluptate " |
| 124 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 125 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 126 | "mollit anim id est laborum." |
| 127 | " Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. " |
| 128 | "Nullam " |
| 129 | "varius, turpis et commodo pharetra, est eros bibendum elit, nec " |
| 130 | "luctus " |
| 131 | "magna felis sollicitudin mauris. Integer in mauris eu nibh euismod " |
| 132 | "gravida. Duis ac tellus et risus vulputate vehicula. Donec lobortis " |
| 133 | "risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor " |
| 134 | "congue, " |
| 135 | "eros est euismod turpis, id tincidunt sapien risus a quam. Maecenas " |
| 136 | "fermentum consequat mi. Donec fermentum. Pellentesque malesuada " |
| 137 | "nulla a " |
| 138 | "mi. Duis sapien sem, aliquet nec, commodo eget, consequat quis, " |
| 139 | "neque. " |
| 140 | "Aliquam faucibus, elit ut dictum aliquet, felis nisl adipiscing " |
| 141 | "sapien, " |
| 142 | "sed malesuada diam lacus eget erat. Cras mollis scelerisque nunc. " |
| 143 | "Nullam " |
| 144 | "arcu. Aliquam consequat. Curabitur augue lorem, dapibus quis, " |
| 145 | "laoreet " |
| 146 | "et, pretium ac, nisi. Aenean magna nisl, mollis quis, molestie eu, " |
| 147 | "feugiat in, orci. In hac habitasse platea dictumst."); |
| 148 | std::string password_2( |
| 149 | "Aliquam faucibus, elit ut dictum aliquet, felis nisl"); |
| 150 | std::string iv2("87654321hgfedcba"); |
| 151 | std::unique_ptr<unsigned char[]> output, output_2_2; |
| 152 | size_t output_2_size, output_2_2_size; |
| 153 | output = std::make_unique<unsigned char[]>(get_ciphertext_size( |
| 154 | source_2.length(), Keyring_aes_opmode::keyring_aes_256_cbc)); |
| 155 | EXPECT_TRUE(output.get() != nullptr); |
| 156 | aes_return_status source_encryption_status = |
| 157 | aes_encrypt(reinterpret_cast<const unsigned char *>(source_2.c_str()), |
| 158 | static_cast<unsigned int>(source_2.length()), output.get(), |
| 159 | reinterpret_cast<const unsigned char *>(password_2.c_str()), |
| 160 | static_cast<unsigned int>(password_2.length()), |
| 161 | Keyring_aes_opmode::keyring_aes_256_cbc, |
| 162 | reinterpret_cast<const unsigned char *>(iv2.c_str()), true, |
| 163 | &output_2_size); |
| 164 | EXPECT_TRUE(source_encryption_status == aes_return_status::AES_OP_OK); |
| 165 | |
| 166 | output_2_2 = std::make_unique<unsigned char[]>(get_ciphertext_size( |
| 167 | source_2.length(), Keyring_aes_opmode::keyring_aes_256_cbc)); |
| 168 | EXPECT_TRUE(output_2_2.get() != nullptr); |
| 169 | aes_return_status source_decryption_status = aes_decrypt( |
| 170 | output.get(), |
| 171 | static_cast<unsigned int>(get_ciphertext_size( |
| 172 | source_2.length(), Keyring_aes_opmode::keyring_aes_256_cbc)), |
| 173 | output_2_2.get(), |
| 174 | reinterpret_cast<const unsigned char *>(password_2.c_str()), |
| 175 | static_cast<unsigned int>(password_2.length()), |
| 176 | Keyring_aes_opmode::keyring_aes_256_cbc, |
| 177 | reinterpret_cast<const unsigned char *>(iv2.c_str()), true, |
| 178 | &output_2_2_size); |
| 179 | EXPECT_TRUE(source_decryption_status == aes_return_status::AES_OP_OK); |
| 180 | |
| 181 | std::string decrypted_source(reinterpret_cast<char *>(output_2_2.get()), |
| 182 | output_2_2_size); |
| 183 | |
| 184 | EXPECT_TRUE(decrypted_source == source_2); |
| 185 | } |
| 186 | |
| 187 | TEST_F(AESEncryption_test, EncryptDecryptFileTest) { |
| 188 | /* Encrypt the content */ |
| 189 | std::string source("quick brown fox jumped over the lazy dog"); |
| 190 | std::string password("pass1234"); |
| 191 | std::string iv("87654321hgfedcba"); |
| 192 | std::unique_ptr<unsigned char[]> output, output_2; |
| 193 | size_t output_size, output_2_size; |
| 194 | output = std::make_unique<unsigned char[]>(get_ciphertext_size( |
| 195 | source.length(), Keyring_aes_opmode::keyring_aes_256_cbc)); |
| 196 | EXPECT_TRUE(output.get() != nullptr); |
| 197 | aes_return_status source_encryption_status = aes_encrypt( |
| 198 | reinterpret_cast<const unsigned char *>(source.c_str()), |
| 199 | static_cast<unsigned int>(source.length()), output.get(), |
| 200 | reinterpret_cast<const unsigned char *>(password.c_str()), |
| 201 | static_cast<unsigned int>(password.length()), |
| 202 | Keyring_aes_opmode::keyring_aes_256_cbc, |
| 203 | reinterpret_cast<const unsigned char *>(iv.c_str()), true, &output_size); |
| 204 | EXPECT_TRUE(source_encryption_status == aes_return_status::AES_OP_OK); |
| 205 | |
| 206 | /* Write the content to file and then read it */ |
| 207 | std::string file_name("encrypt_decrypt_file_test"); |
| 208 | // Content: IV + Encrypted Data |
| 209 | std::string data(iv.c_str(), iv.length()); |
| 210 | data.append(reinterpret_cast<char *>(output.get()), output_size); |
| 211 | EXPECT_TRUE(data.length() == (iv.length() + output_size)); |
| 212 | |
| 213 | File_writer file_writer(file_name, data); |
| 214 | ASSERT_TRUE(file_writer.valid()); |
| 215 | |
| 216 | std::string read_data; |
| 217 | File_reader file_reader(file_name, true, read_data); |
| 218 | ASSERT_TRUE(file_reader.valid()); |
| 219 | |
| 220 | ASSERT_TRUE(read_data == data); |
| 221 | |
| 222 | output_2 = std::make_unique<unsigned char[]>( |
| 223 | get_ciphertext_size(read_data.length() - iv.length(), |
| 224 | Keyring_aes_opmode::keyring_aes_256_cbc)); |
| 225 | EXPECT_TRUE(output_2.get() != nullptr); |
| 226 | aes_return_status source_decryption_status = aes_decrypt( |
| 227 | reinterpret_cast<const unsigned char *>(read_data.c_str()) + iv.length(), |
| 228 | static_cast<unsigned int>(read_data.length() - iv.length()), |
| 229 | output_2.get(), reinterpret_cast<const unsigned char *>(password.c_str()), |
| 230 | static_cast<unsigned int>(password.length()), |
| 231 | Keyring_aes_opmode::keyring_aes_256_cbc, |
| 232 | reinterpret_cast<const unsigned char *>(read_data.c_str()), true, |
| 233 | &output_2_size); |
| 234 | EXPECT_TRUE(source_decryption_status == aes_return_status::AES_OP_OK); |
| 235 | |
| 236 | std::string decrypted_source(reinterpret_cast<char *>(output_2.get()), |
| 237 | output_2_size); |
| 238 | |
| 239 | EXPECT_TRUE(decrypted_source == source); |
| 240 | |
| 241 | ASSERT_TRUE(std::remove(file_name.c_str()) == 0); |
| 242 | } |
| 243 | |
| 244 | } // namespace aes_encryption_unittest |