| 1 | /* Copyright (c) 2017, 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 <gtest/gtest.h> |
| 25 | |
| 26 | #include "plugin/x/client/authentication/sha256_scramble_generator.h" |
| 27 | |
| 28 | namespace xcl { |
| 29 | namespace sha256_scramble_unittest { |
| 30 | |
| 31 | using namespace xcl::sha256_password; |
| 32 | |
| 33 | TEST(SHA256_digest_test, init_digest_context) { |
| 34 | SHA256_digest sha256_digest; |
| 35 | ASSERT_TRUE(sha256_digest.all_ok()); |
| 36 | } |
| 37 | |
| 38 | TEST(SHA256_digest_test, digest_single_stage) { |
| 39 | SHA256_digest sha256_digest; |
| 40 | const char *plaintext1 = "quick brown fox jumped over the lazy dog"; |
| 41 | size_t plaintext1_length = strlen(plaintext1); |
| 42 | unsigned char expected_digest1[] = { |
| 43 | 0x5c, 0xa1, 0xa2, 0x08, 0xc4, 0x61, 0x95, 0x3f, 0x7c, 0x12, 0xd2, |
| 44 | 0x79, 0xc7, 0xa8, 0x62, 0x0f, 0x3f, 0x83, 0x87, 0xf4, 0x09, 0x8c, |
| 45 | 0xe8, 0x53, 0x1b, 0x17, 0x51, 0x03, 0x37, 0xbd, 0x63, 0x78}; |
| 46 | |
| 47 | unsigned char digest_output[CACHING_SHA2_DIGEST_LENGTH]; |
| 48 | |
| 49 | ASSERT_TRUE(sha256_digest.all_ok()); |
| 50 | ASSERT_FALSE(sha256_digest.update_digest(plaintext1, plaintext1_length)); |
| 51 | ASSERT_FALSE( |
| 52 | sha256_digest.retrieve_digest(digest_output, sizeof(digest_output))); |
| 53 | ASSERT_TRUE( |
| 54 | memcmp(expected_digest1, digest_output, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 55 | sha256_digest.scrub(); |
| 56 | |
| 57 | const char *plaintext2 = "ABCD1234%^&*"; |
| 58 | size_t plaintext2_length = strlen(plaintext2); |
| 59 | |
| 60 | unsigned char expected_digest2[] = { |
| 61 | 0xdd, 0xaf, 0x2b, 0xa0, 0x31, 0x73, 0x45, 0x3d, 0x72, 0x34, 0x51, |
| 62 | 0x27, 0xeb, 0x32, 0xb5, 0x24, 0x00, 0xc8, 0xf0, 0x89, 0xcb, 0x8a, |
| 63 | 0xd6, 0xce, 0x3c, 0x67, 0x37, 0xac, 0x01, 0x45, 0x84, 0x09}; |
| 64 | ASSERT_TRUE(sha256_digest.all_ok()); |
| 65 | ASSERT_FALSE(sha256_digest.update_digest(plaintext2, plaintext2_length)); |
| 66 | ASSERT_FALSE( |
| 67 | sha256_digest.retrieve_digest(digest_output, sizeof(digest_output))); |
| 68 | ASSERT_TRUE( |
| 69 | memcmp(expected_digest2, digest_output, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 70 | |
| 71 | sha256_digest.scrub(); |
| 72 | } |
| 73 | |
| 74 | TEST(SHA256_digest_test, digest_multi_stage) { |
| 75 | SHA256_digest sha256_digest; |
| 76 | const char *plaintext_input1 = "quick brown fox jumped over the lazy dog"; |
| 77 | size_t plaintext_input1_length = strlen(plaintext_input1); |
| 78 | const char *plaintext_input2 = "ABCD1234%^&*"; |
| 79 | size_t plaintext_input2_length = strlen(plaintext_input2); |
| 80 | const char *plaintext_input3 = "Hello World"; |
| 81 | size_t plaintext_input3_length = strlen(plaintext_input3); |
| 82 | |
| 83 | unsigned char digest_output[CACHING_SHA2_DIGEST_LENGTH]; |
| 84 | |
| 85 | unsigned char expected_digest1[] = { |
| 86 | 0xd5, 0x66, 0x19, 0xff, 0x7c, 0xdb, 0x13, 0x00, 0x0b, 0x57, 0x1c, |
| 87 | 0x33, 0x8f, 0xe3, 0x33, 0xaf, 0x41, 0x87, 0xa6, 0x83, 0x03, 0x31, |
| 88 | 0x1b, 0xb6, 0x64, 0x7b, 0x6f, 0xbe, 0x6e, 0xf0, 0x99, 0xd9}; |
| 89 | |
| 90 | ASSERT_TRUE(sha256_digest.all_ok()); |
| 91 | ASSERT_FALSE( |
| 92 | sha256_digest.update_digest(plaintext_input1, plaintext_input1_length)); |
| 93 | ASSERT_FALSE( |
| 94 | sha256_digest.update_digest(plaintext_input2, plaintext_input2_length)); |
| 95 | ASSERT_FALSE( |
| 96 | sha256_digest.update_digest(plaintext_input3, plaintext_input3_length)); |
| 97 | ASSERT_FALSE( |
| 98 | sha256_digest.retrieve_digest(digest_output, sizeof(digest_output))); |
| 99 | ASSERT_TRUE( |
| 100 | memcmp(expected_digest1, digest_output, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 101 | sha256_digest.scrub(); |
| 102 | |
| 103 | unsigned char expected_digest2[] = { |
| 104 | 0xd0, 0xcf, 0xa1, 0xd2, 0x9f, 0xb6, 0xfe, 0x8f, 0x3c, 0xff, 0x2c, |
| 105 | 0xa8, 0x2a, 0xe2, 0xc6, 0x6b, 0x8b, 0x2b, 0x33, 0xf9, 0x38, 0x35, |
| 106 | 0xc7, 0xae, 0x6a, 0xfc, 0x28, 0x85, 0x5d, 0xd3, 0xe5, 0xc5}; |
| 107 | ASSERT_TRUE(sha256_digest.all_ok()); |
| 108 | ASSERT_FALSE( |
| 109 | sha256_digest.update_digest(plaintext_input1, plaintext_input1_length)); |
| 110 | ASSERT_FALSE( |
| 111 | sha256_digest.update_digest(plaintext_input3, plaintext_input3_length)); |
| 112 | ASSERT_FALSE( |
| 113 | sha256_digest.update_digest(plaintext_input2, plaintext_input2_length)); |
| 114 | ASSERT_FALSE( |
| 115 | sha256_digest.retrieve_digest(digest_output, sizeof(digest_output))); |
| 116 | ASSERT_TRUE( |
| 117 | memcmp(expected_digest2, digest_output, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 118 | |
| 119 | sha256_digest.scrub(); |
| 120 | } |
| 121 | |
| 122 | TEST(SHA256_digest_test, generate_scramble) { |
| 123 | std::string source = "Ab12#$Cd56&*"; |
| 124 | std::string rnd = "eF!@34gH%^78"; |
| 125 | unsigned char scramble[CACHING_SHA2_DIGEST_LENGTH]; |
| 126 | |
| 127 | Generate_scramble generate_scramble(source, rnd); |
| 128 | |
| 129 | unsigned char expected_scramble1[] = { |
| 130 | 0x6a, 0x45, 0x37, 0x96, 0x6b, 0x29, 0x63, 0x59, 0x24, 0x8d, 0x64, |
| 131 | 0x86, 0x0a, 0xd6, 0xcc, 0x2a, 0x06, 0x47, 0x8c, 0x26, 0xea, 0xaa, |
| 132 | 0x3b, 0x02, 0x69, 0x4c, 0x85, 0x02, 0xf5, 0x5b, 0xc8, 0xdc}; |
| 133 | |
| 134 | ASSERT_FALSE(generate_scramble.scramble(scramble, sizeof(scramble))); |
| 135 | ASSERT_TRUE( |
| 136 | memcmp(expected_scramble1, scramble, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 137 | } |
| 138 | |
| 139 | TEST(SHA256_digest_test, generate_sha256_scramble) { |
| 140 | std::string source = "Ab12#$Cd56&*"; |
| 141 | std::string rnd = "eF!@34gH%^78"; |
| 142 | |
| 143 | unsigned char scramble[CACHING_SHA2_DIGEST_LENGTH]; |
| 144 | |
| 145 | unsigned char expected_scramble1[] = { |
| 146 | 0x6a, 0x45, 0x37, 0x96, 0x6b, 0x29, 0x63, 0x59, 0x24, 0x8d, 0x64, |
| 147 | 0x86, 0x0a, 0xd6, 0xcc, 0x2a, 0x06, 0x47, 0x8c, 0x26, 0xea, 0xaa, |
| 148 | 0x3b, 0x02, 0x69, 0x4c, 0x85, 0x02, 0xf5, 0x5b, 0xc8, 0xdc}; |
| 149 | |
| 150 | ASSERT_FALSE(generate_sha256_scramble(scramble, CACHING_SHA2_DIGEST_LENGTH, |
| 151 | source.c_str(), source.length(), |
| 152 | rnd.c_str(), rnd.length())); |
| 153 | |
| 154 | ASSERT_TRUE( |
| 155 | memcmp(expected_scramble1, scramble, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 156 | } |
| 157 | |
| 158 | } // namespace sha256_scramble_unittest |
| 159 | } // namespace xcl |