| 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 "my_config.h" |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "crypt_genhash_impl.h" |
| 29 | #include "mysql/service_mysql_alloc.h" |
| 30 | #include "sql/auth/i_sha2_password.h" |
| 31 | #include "sql/auth/sha2_password_common.h" |
| 32 | #include "unittest/gunit/test_utils.h" |
| 33 | |
| 34 | namespace sha2_password_unittest { |
| 35 | using namespace sha2_password; |
| 36 | using my_testing::Server_initializer; |
| 37 | |
| 38 | class SHA256_digestTest : public ::testing::Test { |
| 39 | void SetUp() override { initializer.SetUp(); } |
| 40 | void TearDown() override { initializer.TearDown(); } |
| 41 | |
| 42 | THD *thd() { return initializer.thd(); } |
| 43 | |
| 44 | Server_initializer initializer; |
| 45 | }; |
| 46 | |
| 47 | template <typename T> |
| 48 | void print_hex(T digest) { |
| 49 | std::cout << std::endl << "Generated digest:"; |
| 50 | for (uint i = 0; i < CACHING_SHA2_DIGEST_LENGTH; i++) |
| 51 | printf("0x%02x ", digest.str[i]); |
| 52 | std::cout << std::endl; |
| 53 | } |
| 54 | |
| 55 | static void make_hash_key(const char *username, const char *hostname, |
| 56 | std::string &key) { |
| 57 | key.clear(); |
| 58 | key.append(username ? username : ""); |
| 59 | key.push_back('\0'); |
| 60 | key.append(hostname ? hostname : ""); |
| 61 | key.push_back('\0'); |
| 62 | } |
| 63 | |
| 64 | TEST_F(SHA256_digestTest, InitDigestContext) { |
| 65 | SHA256_digest sha256_digest; |
| 66 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 67 | } |
| 68 | |
| 69 | TEST_F(SHA256_digestTest, DigestSingleStage) { |
| 70 | SHA256_digest sha256_digest; |
| 71 | const char *plaintext1 = "quick brown fox jumped over the lazy dog"; |
| 72 | size_t plaintext1_length = strlen(plaintext1); |
| 73 | unsigned char expected_digest1[] = { |
| 74 | 0x5c, 0xa1, 0xa2, 0x08, 0xc4, 0x61, 0x95, 0x3f, 0x7c, 0x12, 0xd2, |
| 75 | 0x79, 0xc7, 0xa8, 0x62, 0x0f, 0x3f, 0x83, 0x87, 0xf4, 0x09, 0x8c, |
| 76 | 0xe8, 0x53, 0x1b, 0x17, 0x51, 0x03, 0x37, 0xbd, 0x63, 0x78}; |
| 77 | |
| 78 | unsigned char digest_output[CACHING_SHA2_DIGEST_LENGTH]; |
| 79 | |
| 80 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 81 | ASSERT_TRUE(sha256_digest.update_digest(plaintext1, plaintext1_length) == |
| 82 | false); |
| 83 | ASSERT_TRUE(sha256_digest.retrieve_digest(digest_output, |
| 84 | sizeof(digest_output)) == false); |
| 85 | ASSERT_TRUE( |
| 86 | memcmp(expected_digest1, digest_output, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 87 | sha256_digest.scrub(); |
| 88 | |
| 89 | const char *plaintext2 = "ABCD1234%^&*"; |
| 90 | size_t plaintext2_length = strlen(plaintext2); |
| 91 | |
| 92 | unsigned char expected_digest2[] = { |
| 93 | 0xdd, 0xaf, 0x2b, 0xa0, 0x31, 0x73, 0x45, 0x3d, 0x72, 0x34, 0x51, |
| 94 | 0x27, 0xeb, 0x32, 0xb5, 0x24, 0x00, 0xc8, 0xf0, 0x89, 0xcb, 0x8a, |
| 95 | 0xd6, 0xce, 0x3c, 0x67, 0x37, 0xac, 0x01, 0x45, 0x84, 0x09}; |
| 96 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 97 | ASSERT_TRUE(sha256_digest.update_digest(plaintext2, plaintext2_length) == |
| 98 | false); |
| 99 | ASSERT_TRUE(sha256_digest.retrieve_digest(digest_output, |
| 100 | sizeof(digest_output)) == false); |
| 101 | ASSERT_TRUE( |
| 102 | memcmp(expected_digest2, digest_output, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 103 | |
| 104 | sha256_digest.scrub(); |
| 105 | } |
| 106 | |
| 107 | TEST_F(SHA256_digestTest, DigestMultiStage) { |
| 108 | SHA256_digest sha256_digest; |
| 109 | const char *plaintext_input1 = "quick brown fox jumped over the lazy dog"; |
| 110 | size_t plaintext_input1_length = strlen(plaintext_input1); |
| 111 | const char *plaintext_input2 = "ABCD1234%^&*"; |
| 112 | size_t plaintext_input2_length = strlen(plaintext_input2); |
| 113 | const char *plaintext_input3 = "Hello World"; |
| 114 | size_t plaintext_input3_length = strlen(plaintext_input3); |
| 115 | |
| 116 | unsigned char digest_output[CACHING_SHA2_DIGEST_LENGTH]; |
| 117 | |
| 118 | unsigned char expected_digest1[] = { |
| 119 | 0xd5, 0x66, 0x19, 0xff, 0x7c, 0xdb, 0x13, 0x00, 0x0b, 0x57, 0x1c, |
| 120 | 0x33, 0x8f, 0xe3, 0x33, 0xaf, 0x41, 0x87, 0xa6, 0x83, 0x03, 0x31, |
| 121 | 0x1b, 0xb6, 0x64, 0x7b, 0x6f, 0xbe, 0x6e, 0xf0, 0x99, 0xd9}; |
| 122 | |
| 123 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 124 | ASSERT_TRUE(sha256_digest.update_digest(plaintext_input1, |
| 125 | plaintext_input1_length) == false); |
| 126 | ASSERT_TRUE(sha256_digest.update_digest(plaintext_input2, |
| 127 | plaintext_input2_length) == false); |
| 128 | ASSERT_TRUE(sha256_digest.update_digest(plaintext_input3, |
| 129 | plaintext_input3_length) == false); |
| 130 | ASSERT_TRUE(sha256_digest.retrieve_digest(digest_output, |
| 131 | sizeof(digest_output)) == false); |
| 132 | ASSERT_TRUE( |
| 133 | memcmp(expected_digest1, digest_output, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 134 | sha256_digest.scrub(); |
| 135 | |
| 136 | unsigned char expected_digest2[] = { |
| 137 | 0xd0, 0xcf, 0xa1, 0xd2, 0x9f, 0xb6, 0xfe, 0x8f, 0x3c, 0xff, 0x2c, |
| 138 | 0xa8, 0x2a, 0xe2, 0xc6, 0x6b, 0x8b, 0x2b, 0x33, 0xf9, 0x38, 0x35, |
| 139 | 0xc7, 0xae, 0x6a, 0xfc, 0x28, 0x85, 0x5d, 0xd3, 0xe5, 0xc5}; |
| 140 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 141 | ASSERT_TRUE(sha256_digest.update_digest(plaintext_input1, |
| 142 | plaintext_input1_length) == false); |
| 143 | ASSERT_TRUE(sha256_digest.update_digest(plaintext_input3, |
| 144 | plaintext_input3_length) == false); |
| 145 | ASSERT_TRUE(sha256_digest.update_digest(plaintext_input2, |
| 146 | plaintext_input2_length) == false); |
| 147 | ASSERT_TRUE(sha256_digest.retrieve_digest(digest_output, |
| 148 | sizeof(digest_output)) == false); |
| 149 | ASSERT_TRUE( |
| 150 | memcmp(expected_digest2, digest_output, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 151 | |
| 152 | sha256_digest.scrub(); |
| 153 | } |
| 154 | |
| 155 | TEST_F(SHA256_digestTest, GenerateScramble) { |
| 156 | std::string source = "Ab12#$Cd56&*"; |
| 157 | std::string rnd = "eF!@34gH%^78"; |
| 158 | unsigned char scramble[CACHING_SHA2_DIGEST_LENGTH]; |
| 159 | |
| 160 | Generate_scramble generate_scramble(source, rnd); |
| 161 | |
| 162 | unsigned char expected_scramble1[] = { |
| 163 | 0x6a, 0x45, 0x37, 0x96, 0x6b, 0x29, 0x63, 0x59, 0x24, 0x8d, 0x64, |
| 164 | 0x86, 0x0a, 0xd6, 0xcc, 0x2a, 0x06, 0x47, 0x8c, 0x26, 0xea, 0xaa, |
| 165 | 0x3b, 0x02, 0x69, 0x4c, 0x85, 0x02, 0xf5, 0x5b, 0xc8, 0xdc}; |
| 166 | |
| 167 | ASSERT_TRUE(generate_scramble.scramble(scramble, sizeof(scramble)) == false); |
| 168 | ASSERT_TRUE( |
| 169 | memcmp(expected_scramble1, scramble, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 170 | } |
| 171 | |
| 172 | TEST_F(SHA256_digestTest, ValidateScramble) { |
| 173 | SHA256_digest sha256_digest; |
| 174 | |
| 175 | unsigned char received_scramble1[] = { |
| 176 | 0x6a, 0x45, 0x37, 0x96, 0x6b, 0x29, 0x63, 0x59, 0x24, 0x8d, 0x64, |
| 177 | 0x86, 0x0a, 0xd6, 0xcc, 0x2a, 0x06, 0x47, 0x8c, 0x26, 0xea, 0xaa, |
| 178 | 0x3b, 0x02, 0x69, 0x4c, 0x85, 0x02, 0xf5, 0x5b, 0xc8, 0xdc}; |
| 179 | |
| 180 | const char *source = "Ab12#$Cd56&*"; |
| 181 | size_t source_length = strlen(source); |
| 182 | std::string rnd = "eF!@34gH%^78"; |
| 183 | |
| 184 | unsigned char digest_stage2[CACHING_SHA2_DIGEST_LENGTH]; |
| 185 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 186 | ASSERT_TRUE(sha256_digest.update_digest(source, source_length) == false); |
| 187 | ASSERT_TRUE(sha256_digest.retrieve_digest(digest_stage2, |
| 188 | sizeof(digest_stage2)) == false); |
| 189 | sha256_digest.scrub(); |
| 190 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 191 | ASSERT_TRUE(sha256_digest.update_digest(digest_stage2, |
| 192 | sizeof(digest_stage2)) == false); |
| 193 | ASSERT_TRUE(sha256_digest.retrieve_digest(digest_stage2, |
| 194 | sizeof(digest_stage2)) == false); |
| 195 | sha256_digest.scrub(); |
| 196 | |
| 197 | Validate_scramble validate_scramble( |
| 198 | received_scramble1, digest_stage2, |
| 199 | reinterpret_cast<const unsigned char *>(rnd.c_str()), rnd.length()); |
| 200 | |
| 201 | ASSERT_TRUE(validate_scramble.validate() == false); |
| 202 | } |
| 203 | |
| 204 | TEST_F(SHA256_digestTest, generate_sha256_scramble) { |
| 205 | std::string source = "Ab12#$Cd56&*"; |
| 206 | std::string rnd = "eF!@34gH%^78"; |
| 207 | |
| 208 | unsigned char scramble[CACHING_SHA2_DIGEST_LENGTH]; |
| 209 | |
| 210 | unsigned char expected_scramble1[] = { |
| 211 | 0x6a, 0x45, 0x37, 0x96, 0x6b, 0x29, 0x63, 0x59, 0x24, 0x8d, 0x64, |
| 212 | 0x86, 0x0a, 0xd6, 0xcc, 0x2a, 0x06, 0x47, 0x8c, 0x26, 0xea, 0xaa, |
| 213 | 0x3b, 0x02, 0x69, 0x4c, 0x85, 0x02, 0xf5, 0x5b, 0xc8, 0xdc}; |
| 214 | |
| 215 | ASSERT_TRUE(generate_sha256_scramble(scramble, CACHING_SHA2_DIGEST_LENGTH, |
| 216 | source.c_str(), source.length(), |
| 217 | rnd.c_str(), rnd.length()) == false); |
| 218 | |
| 219 | ASSERT_TRUE( |
| 220 | memcmp(expected_scramble1, scramble, CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 221 | } |
| 222 | |
| 223 | TEST_F(SHA256_digestTest, validate_sha256_scramble) { |
| 224 | SHA256_digest sha256_digest; |
| 225 | |
| 226 | unsigned char received_scramble1[] = { |
| 227 | 0x6a, 0x45, 0x37, 0x96, 0x6b, 0x29, 0x63, 0x59, 0x24, 0x8d, 0x64, |
| 228 | 0x86, 0x0a, 0xd6, 0xcc, 0x2a, 0x06, 0x47, 0x8c, 0x26, 0xea, 0xaa, |
| 229 | 0x3b, 0x02, 0x69, 0x4c, 0x85, 0x02, 0xf5, 0x5b, 0xc8, 0xdc}; |
| 230 | |
| 231 | const char *source = "Ab12#$Cd56&*"; |
| 232 | size_t source_length = strlen(source); |
| 233 | const unsigned char *rnd = (const unsigned char *)"eF!@34gH%^78"; |
| 234 | size_t rnd_length = strlen(pointer_cast<const char *>(rnd)); |
| 235 | |
| 236 | unsigned char digest_stage2[CACHING_SHA2_DIGEST_LENGTH]; |
| 237 | |
| 238 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 239 | ASSERT_TRUE(sha256_digest.update_digest(source, source_length) == false); |
| 240 | ASSERT_TRUE(sha256_digest.retrieve_digest( |
| 241 | digest_stage2, CACHING_SHA2_DIGEST_LENGTH) == false); |
| 242 | sha256_digest.scrub(); |
| 243 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 244 | ASSERT_TRUE(sha256_digest.update_digest(digest_stage2, |
| 245 | CACHING_SHA2_DIGEST_LENGTH) == false); |
| 246 | ASSERT_TRUE(sha256_digest.retrieve_digest( |
| 247 | digest_stage2, CACHING_SHA2_DIGEST_LENGTH) == false); |
| 248 | sha256_digest.scrub(); |
| 249 | |
| 250 | ASSERT_TRUE(validate_sha256_scramble( |
| 251 | received_scramble1, sizeof(received_scramble1), digest_stage2, |
| 252 | sizeof(digest_stage2), rnd, rnd_length) == false); |
| 253 | } |
| 254 | |
| 255 | TEST_F(SHA256_digestTest, SHA2_password_cache) { |
| 256 | SHA2_password_cache sha2_password_cache; |
| 257 | |
| 258 | std::string auth_id1; |
| 259 | std::string auth_id1_u; |
| 260 | std::string auth_id2; |
| 261 | std::string auth_id3; |
| 262 | std::string auth_no_user; |
| 263 | std::string auth_no_host; |
| 264 | std::string auth_empty; |
| 265 | std::string auth_id1_v1; |
| 266 | std::string auth_id1_v2; |
| 267 | |
| 268 | make_hash_key("arthur", "dent.com", auth_id1); |
| 269 | make_hash_key("Arthur", "Dent.com", auth_id1_u); |
| 270 | make_hash_key("marvin", "theparanoidandroid.com", auth_id2); |
| 271 | make_hash_key("zaphod", "beeblebrox", auth_id3); |
| 272 | make_hash_key("", "arthurdent.com", auth_no_user); |
| 273 | make_hash_key("arthurdent.com", "", auth_no_host); |
| 274 | make_hash_key("", "", auth_empty); |
| 275 | |
| 276 | sha2_cache_entry stored_hash; |
| 277 | sha2_cache_entry retrieved_password; |
| 278 | { |
| 279 | SHA256_digest sha256_digest; |
| 280 | |
| 281 | std::string source("HahaH0hO1234#$@#%"); |
| 282 | |
| 283 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 284 | ASSERT_TRUE(sha256_digest.update_digest(source.c_str(), source.length()) == |
| 285 | false); |
| 286 | ASSERT_TRUE(sha256_digest.retrieve_digest(stored_hash.digest_buffer[0], |
| 287 | CACHING_SHA2_DIGEST_LENGTH) == |
| 288 | false); |
| 289 | sha256_digest.scrub(); |
| 290 | ASSERT_TRUE(sha256_digest.update_digest(stored_hash.digest_buffer[0], |
| 291 | CACHING_SHA2_DIGEST_LENGTH) == |
| 292 | false); |
| 293 | ASSERT_TRUE(sha256_digest.retrieve_digest(stored_hash.digest_buffer[0], |
| 294 | CACHING_SHA2_DIGEST_LENGTH) == |
| 295 | false); |
| 296 | sha256_digest.scrub(); |
| 297 | ASSERT_TRUE(sha256_digest.all_ok() == true); |
| 298 | } |
| 299 | |
| 300 | ASSERT_TRUE(sha2_password_cache.size() == 0); |
| 301 | ASSERT_TRUE(sha2_password_cache.add(auth_id1, stored_hash) == false); |
| 302 | ASSERT_TRUE(sha2_password_cache.search(auth_id1, retrieved_password) == |
| 303 | false); |
| 304 | ASSERT_TRUE(memcmp((void *)stored_hash.digest_buffer[0], |
| 305 | (void *)retrieved_password.digest_buffer[0], |
| 306 | CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 307 | ASSERT_TRUE(sha2_password_cache.remove(auth_id1) == false); |
| 308 | ASSERT_TRUE(sha2_password_cache.add(auth_id1, stored_hash) == false); |
| 309 | ASSERT_TRUE(sha2_password_cache.size() == 1); |
| 310 | |
| 311 | ASSERT_TRUE(sha2_password_cache.search(auth_no_user, retrieved_password) == |
| 312 | true); |
| 313 | ASSERT_TRUE(sha2_password_cache.search(auth_no_host, retrieved_password) == |
| 314 | true); |
| 315 | ASSERT_TRUE(sha2_password_cache.search(auth_empty, retrieved_password) == |
| 316 | true); |
| 317 | |
| 318 | ASSERT_TRUE(sha2_password_cache.add(auth_id1, stored_hash) == true); |
| 319 | ASSERT_TRUE(sha2_password_cache.size() == 1); |
| 320 | |
| 321 | ASSERT_TRUE(sha2_password_cache.add(auth_id2, stored_hash) == false); |
| 322 | ASSERT_TRUE(sha2_password_cache.add(auth_id3, stored_hash) == false); |
| 323 | ASSERT_TRUE(sha2_password_cache.size() == 3); |
| 324 | |
| 325 | ASSERT_TRUE(sha2_password_cache.remove(auth_id2) == false); |
| 326 | ASSERT_TRUE(sha2_password_cache.size() == 2); |
| 327 | |
| 328 | ASSERT_TRUE(sha2_password_cache.add(auth_id1_u, stored_hash) == false); |
| 329 | ASSERT_TRUE(sha2_password_cache.size() == 3); |
| 330 | |
| 331 | ASSERT_TRUE(sha2_password_cache.search(auth_id1, retrieved_password) == |
| 332 | false); |
| 333 | ASSERT_TRUE(memcmp((void *)stored_hash.digest_buffer[0], |
| 334 | (void *)retrieved_password.digest_buffer[0], |
| 335 | CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 336 | |
| 337 | ASSERT_TRUE(sha2_password_cache.search(auth_id3, retrieved_password) == |
| 338 | false); |
| 339 | ASSERT_TRUE(memcmp((void *)stored_hash.digest_buffer[0], |
| 340 | (void *)retrieved_password.digest_buffer[0], |
| 341 | CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 342 | |
| 343 | ASSERT_TRUE(sha2_password_cache.search(auth_id2, retrieved_password) == true); |
| 344 | ASSERT_TRUE(sha2_password_cache.remove(auth_id2) == true); |
| 345 | |
| 346 | ASSERT_TRUE(sha2_password_cache.add(auth_empty, stored_hash) == false); |
| 347 | ASSERT_TRUE(sha2_password_cache.search(auth_empty, retrieved_password) == |
| 348 | false); |
| 349 | ASSERT_TRUE(memcmp((void *)stored_hash.digest_buffer[0], |
| 350 | (void *)retrieved_password.digest_buffer[0], |
| 351 | CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 352 | ASSERT_TRUE(sha2_password_cache.remove(auth_empty) == false); |
| 353 | } |
| 354 | |
| 355 | TEST_F(SHA256_digestTest, Caching_sha2_password_Serialize_Deserialize) { |
| 356 | Caching_sha2_password caching_sha2_password(nullptr, |
| 357 | DEFAULT_STORED_DIGEST_ROUNDS); |
| 358 | |
| 359 | Digest_info digest_type; |
| 360 | |
| 361 | std::string sha256_digest; |
| 362 | std::string salt; |
| 363 | |
| 364 | size_t iterations; |
| 365 | |
| 366 | /* Case 1 : valid string */ |
| 367 | std::string sha256_valid_auth_string1( |
| 368 | "$A$005$ABCDEFGHIJKLMNOPQRSTabcdefgh01234567ijklmnop89012345ABCDEFGH678"); |
| 369 | std::string sha256_valid_auth_string1_serialized; |
| 370 | |
| 371 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 372 | sha256_valid_auth_string1, digest_type, salt, sha256_digest, |
| 373 | iterations) == false); |
| 374 | ASSERT_TRUE(digest_type == Digest_info::SHA256_DIGEST); |
| 375 | ASSERT_TRUE(memcmp(salt.c_str(), "ABCDEFGHIJKLMNOPQRST", SALT_LENGTH) == 0); |
| 376 | ASSERT_TRUE(memcmp(sha256_digest.c_str(), |
| 377 | "abcdefgh01234567ijklmnop89012345ABCDEFGH678", |
| 378 | STORED_SHA256_DIGEST_LENGTH) == 0); |
| 379 | ASSERT_TRUE(iterations == 5000); |
| 380 | |
| 381 | ASSERT_TRUE(caching_sha2_password.serialize( |
| 382 | sha256_valid_auth_string1_serialized, digest_type, salt, |
| 383 | sha256_digest, iterations) == false); |
| 384 | ASSERT_TRUE(sha256_valid_auth_string1 == |
| 385 | sha256_valid_auth_string1_serialized); |
| 386 | |
| 387 | /* Valid string with largest iterations count */ |
| 388 | std::string sha256_valid_auth_string_biggest( |
| 389 | "$A$FFF$ABCDEFGHIJKLMNOPQRSTabcdefgh01234567ijklmnop89012345ABCDEFGH678"); |
| 390 | std::string sha256_valid_auth_string_biggest_serialized; |
| 391 | |
| 392 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 393 | sha256_valid_auth_string_biggest, digest_type, salt, |
| 394 | sha256_digest, iterations) == false); |
| 395 | ASSERT_TRUE(digest_type == Digest_info::SHA256_DIGEST); |
| 396 | ASSERT_TRUE(memcmp(salt.c_str(), "ABCDEFGHIJKLMNOPQRST", SALT_LENGTH) == 0); |
| 397 | ASSERT_TRUE(memcmp(sha256_digest.c_str(), |
| 398 | "abcdefgh01234567ijklmnop89012345ABCDEFGH678", |
| 399 | STORED_SHA256_DIGEST_LENGTH) == 0); |
| 400 | ASSERT_TRUE(iterations == MAX_ITERATIONS); |
| 401 | |
| 402 | ASSERT_TRUE(caching_sha2_password.serialize( |
| 403 | sha256_valid_auth_string_biggest_serialized, digest_type, |
| 404 | salt, sha256_digest, iterations) == false); |
| 405 | ASSERT_TRUE(sha256_valid_auth_string_biggest == |
| 406 | sha256_valid_auth_string_biggest_serialized); |
| 407 | |
| 408 | /* Valid string with special characters */ |
| 409 | std::string sha256_valid_auth_string2( |
| 410 | "$A$20A$01234567ABCDEFGH!@#$abcdefgh01234567ijklm890!@#$%^&*;[-=,+]']+*"); |
| 411 | std::string sha256_valid_auth_string2_serialized; |
| 412 | |
| 413 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 414 | sha256_valid_auth_string2, digest_type, salt, sha256_digest, |
| 415 | iterations) == false); |
| 416 | ASSERT_TRUE(digest_type == Digest_info::SHA256_DIGEST); |
| 417 | ASSERT_TRUE(memcmp(salt.c_str(), "01234567ABCDEFGH!@#$", SALT_LENGTH) == 0); |
| 418 | ASSERT_TRUE(memcmp(sha256_digest.c_str(), |
| 419 | "abcdefgh01234567ijklm890!@#$%^&*;[-=,+]']+*", |
| 420 | STORED_SHA256_DIGEST_LENGTH) == 0); |
| 421 | ASSERT_TRUE(iterations == 0x20A * 1000); |
| 422 | |
| 423 | ASSERT_TRUE(caching_sha2_password.serialize( |
| 424 | sha256_valid_auth_string2_serialized, digest_type, salt, |
| 425 | sha256_digest, iterations) == false); |
| 426 | ASSERT_TRUE(sha256_valid_auth_string2 == |
| 427 | sha256_valid_auth_string2_serialized); |
| 428 | |
| 429 | /* Invalid string with incorrect digest type/length */ |
| 430 | std::string sha256_invalid_digest_type( |
| 431 | "$B$005$01234567ABCDEFGH!@#$abcdefgh01234567ijklm890!@#$%^&*a;3-6;2[6-4"); |
| 432 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 433 | sha256_invalid_digest_type, digest_type, salt, sha256_digest, |
| 434 | iterations) == true); |
| 435 | sha256_invalid_digest_type.assign( |
| 436 | "$HOHO$005$01234567ABCDEFGH!@#$abcdefgh01234567ijklm890!@#$%^&*a;3-6;2[6-" |
| 437 | "4"); |
| 438 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 439 | sha256_invalid_digest_type, digest_type, salt, sha256_digest, |
| 440 | iterations) == true); |
| 441 | |
| 442 | digest_type = Digest_info::DIGEST_LAST; |
| 443 | ASSERT_TRUE(caching_sha2_password.serialize( |
| 444 | sha256_valid_auth_string2_serialized, digest_type, salt, |
| 445 | sha256_digest, iterations) == true); |
| 446 | digest_type = Digest_info::SHA256_DIGEST; |
| 447 | |
| 448 | /* Invalid string with incorrect iteration count */ |
| 449 | std::string sha256_invalid_iteration( |
| 450 | "$A$0$01234567ABCDEFGH!@#$abcdefgh01234567ijklm890!@#$%^&*a;3-6;2[6-4"); |
| 451 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 452 | sha256_invalid_iteration, digest_type, salt, sha256_digest, |
| 453 | iterations) == true); |
| 454 | sha256_invalid_iteration.assign( |
| 455 | "$A$000$01234567ABCDEFGH!@#$abcdefgh01234567ijklm890!@#$%^&*a;3-6;2[6-4"); |
| 456 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 457 | sha256_invalid_iteration, digest_type, salt, sha256_digest, |
| 458 | iterations) == true); |
| 459 | sha256_invalid_iteration.assign( |
| 460 | "$A$01234567ABCDEFGH!@#$abcdefgh01234567ijklm890!@#$%^&*a;3-6;2[6-4"); |
| 461 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 462 | sha256_invalid_iteration, digest_type, salt, sha256_digest, |
| 463 | iterations) == true); |
| 464 | sha256_invalid_iteration.assign( |
| 465 | "$A$$01234567ABCDEFGH!@#$abcdefgh01234567ijklm890!@#$%^&*a;3-6;2[6-4"); |
| 466 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 467 | sha256_invalid_iteration, digest_type, salt, sha256_digest, |
| 468 | iterations) == true); |
| 469 | iterations = 0; |
| 470 | ASSERT_TRUE(caching_sha2_password.serialize( |
| 471 | sha256_valid_auth_string2_serialized, digest_type, salt, |
| 472 | sha256_digest, iterations) == true); |
| 473 | iterations = MAX_ITERATIONS + 1; |
| 474 | ASSERT_TRUE(caching_sha2_password.serialize( |
| 475 | sha256_valid_auth_string2_serialized, digest_type, salt, |
| 476 | sha256_digest, iterations) == true); |
| 477 | iterations = 5000; |
| 478 | /* Invalid string with incorrect salt length */ |
| 479 | std::string sha256_invalid_salt("$A$0005$01234567ABCDE"); |
| 480 | ASSERT_TRUE( |
| 481 | caching_sha2_password.deserialize(sha256_invalid_salt, digest_type, salt, |
| 482 | sha256_digest, iterations) == true); |
| 483 | salt.assign("$A$0005$01234567ABCDEW$% CF#$BBF"); |
| 484 | ASSERT_TRUE(caching_sha2_password.serialize( |
| 485 | sha256_valid_auth_string2_serialized, digest_type, salt, |
| 486 | sha256_digest, iterations) == true); |
| 487 | salt.assign(""); |
| 488 | ASSERT_TRUE(caching_sha2_password.serialize( |
| 489 | sha256_valid_auth_string2_serialized, digest_type, salt, |
| 490 | sha256_digest, iterations) == true); |
| 491 | salt.assign("ABCDEFGHIJKLMNOPQRST"); |
| 492 | /* Invalid string with incorrect digest length */ |
| 493 | std::string sha256_invalid_digest( |
| 494 | "$A$0005$01234567ABCDEFGH!@#$abcdefgh01234567ijklm890!@#$%^&"); |
| 495 | ASSERT_TRUE(caching_sha2_password.deserialize( |
| 496 | sha256_invalid_digest, digest_type, salt, sha256_digest, |
| 497 | iterations) == true); |
| 498 | } |
| 499 | |
| 500 | TEST_F(SHA256_digestTest, Caching_sha2_password_generate_fast_digest) { |
| 501 | Caching_sha2_password caching_sha2_password(nullptr, |
| 502 | DEFAULT_STORED_DIGEST_ROUNDS); |
| 503 | Caching_sha2_password caching_sha2_password_4( |
| 504 | nullptr, DEFAULT_STORED_DIGEST_ROUNDS, 4); |
| 505 | |
| 506 | std::string plaintext("HahaH0hO1234#$@#%"); |
| 507 | sha2_cache_entry digest; |
| 508 | |
| 509 | unsigned char expected_digest[] = { |
| 510 | 0x00, 0x3c, 0x20, 0x68, 0xb3, 0x6b, 0xc9, 0xb7, 0x18, 0x75, 0xb3, |
| 511 | 0x38, 0x7d, 0x2f, 0x86, 0xf9, 0xe3, 0x3a, 0xb3, 0x83, 0x11, 0x6b, |
| 512 | 0xd9, 0x57, 0xf1, 0x73, 0x46, 0x9b, 0x1f, 0x38, 0xf2, 0xdf}; |
| 513 | |
| 514 | unsigned char expected_digest_4[] = { |
| 515 | 0x5c, 0x28, 0x83, 0xc3, 0x7a, 0xf0, 0xe1, 0x69, 0x2a, 0x03, 0xe2, |
| 516 | 0x91, 0x06, 0x4a, 0xf2, 0xa5, 0x05, 0x8b, 0x4a, 0xcd, 0x7d, 0x9c, |
| 517 | 0x90, 0xa5, 0x4b, 0x44, 0x45, 0xf5, 0xfc, 0x33, 0x53, 0xfa}; |
| 518 | |
| 519 | ASSERT_TRUE(caching_sha2_password.generate_fast_digest(plaintext, digest, |
| 520 | 0) == false); |
| 521 | ASSERT_TRUE(memcmp(digest.digest_buffer[0], expected_digest, |
| 522 | CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 523 | |
| 524 | ASSERT_TRUE(caching_sha2_password_4.generate_fast_digest(plaintext, digest, |
| 525 | 0) == false); |
| 526 | ASSERT_TRUE(memcmp(digest.digest_buffer[0], expected_digest_4, |
| 527 | CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 528 | |
| 529 | plaintext.assign(";bCdEF34^i9&*\":({\\56\""); |
| 530 | |
| 531 | unsigned char expected_digest2[] = { |
| 532 | 0x36, 0x87, 0x98, 0x1f, 0xd5, 0x1f, 0xcb, 0x29, 0x66, 0x12, 0x71, |
| 533 | 0x51, 0x2c, 0x84, 0xc5, 0x7e, 0x9f, 0xca, 0x6a, 0xf1, 0xb9, 0x4d, |
| 534 | 0x33, 0x74, 0x8e, 0x38, 0xfe, 0x83, 0x8c, 0x45, 0x2c, 0x51}; |
| 535 | |
| 536 | unsigned char expected_digest2_4[] = { |
| 537 | 0x8f, 0xb6, 0xbe, 0x4e, 0x03, 0xae, 0x07, 0xa2, 0x0c, 0x1d, 0x34, |
| 538 | 0x4c, 0x65, 0x8c, 0x98, 0xfb, 0x72, 0x0a, 0x10, 0xd3, 0x27, 0x91, |
| 539 | 0xf8, 0x73, 0x1b, 0xfc, 0x11, 0xb9, 0x28, 0x09, 0x32, 0x7a}; |
| 540 | |
| 541 | ASSERT_TRUE(caching_sha2_password.generate_fast_digest(plaintext, digest, |
| 542 | 0) == false); |
| 543 | ASSERT_TRUE(memcmp(digest.digest_buffer[0], expected_digest2, |
| 544 | CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 545 | |
| 546 | ASSERT_TRUE(caching_sha2_password_4.generate_fast_digest(plaintext, digest, |
| 547 | 0) == false); |
| 548 | ASSERT_TRUE(memcmp(digest.digest_buffer[0], expected_digest2_4, |
| 549 | CACHING_SHA2_DIGEST_LENGTH) == 0); |
| 550 | } |
| 551 | |
| 552 | TEST_F(SHA256_digestTest, Caching_sha2_password_generate_sha2_multi_hash) { |
| 553 | std::string auth_id_arthur("'arthur'@'dent.com'"); |
| 554 | std::string plaintext_buffer_arthur("HahaH0hO1234#$@#%"); |
| 555 | std::string salt_buffer_arthur("01234567899876543210"); |
| 556 | char stage2[CRYPT_MAX_PASSWORD_SIZE + 1]; |
| 557 | memset(stage2, 0, CRYPT_MAX_PASSWORD_SIZE + 1); |
| 558 | my_crypt_genhash( |
| 559 | stage2, CRYPT_MAX_PASSWORD_SIZE, plaintext_buffer_arthur.c_str(), |
| 560 | plaintext_buffer_arthur.length(), salt_buffer_arthur.c_str(), nullptr); |
| 561 | |
| 562 | std::string digest_string1(stage2); |
| 563 | digest_string1 = digest_string1.substr( |
| 564 | digest_string1.find('$', 3 + CRYPT_SALT_LENGTH) + 1, std::string::npos); |
| 565 | |
| 566 | Caching_sha2_password caching_sha2_password(nullptr, |
| 567 | DEFAULT_STORED_DIGEST_ROUNDS); |
| 568 | std::string digest; |
| 569 | ASSERT_TRUE(caching_sha2_password.generate_sha2_multi_hash( |
| 570 | plaintext_buffer_arthur, salt_buffer_arthur, &digest, 5000) == |
| 571 | false); |
| 572 | ASSERT_TRUE(digest == digest_string1); |
| 573 | |
| 574 | /*Try with random salt */ |
| 575 | char random_salt_buffer[CRYPT_SALT_LENGTH + 1]; |
| 576 | generate_user_salt(random_salt_buffer, CRYPT_SALT_LENGTH + 1); |
| 577 | salt_buffer_arthur.assign(random_salt_buffer, CRYPT_SALT_LENGTH); |
| 578 | |
| 579 | memset(stage2, 0, CRYPT_MAX_PASSWORD_SIZE + 1); |
| 580 | my_crypt_genhash( |
| 581 | stage2, CRYPT_MAX_PASSWORD_SIZE, plaintext_buffer_arthur.c_str(), |
| 582 | plaintext_buffer_arthur.length(), salt_buffer_arthur.c_str(), nullptr); |
| 583 | digest_string1.assign(stage2); |
| 584 | digest_string1 = digest_string1.substr( |
| 585 | digest_string1.find('$', 3 + CRYPT_SALT_LENGTH) + 1, std::string::npos); |
| 586 | |
| 587 | ASSERT_TRUE(caching_sha2_password.generate_sha2_multi_hash( |
| 588 | plaintext_buffer_arthur, salt_buffer_arthur, &digest, 5000) == |
| 589 | false); |
| 590 | ASSERT_TRUE(digest == digest_string1); |
| 591 | } |
| 592 | |
| 593 | TEST_F(SHA256_digestTest, |
| 594 | Caching_sha2_password_authenticate_fast_authenticate) { |
| 595 | Caching_sha2_password caching_sha2_password(nullptr, |
| 596 | DEFAULT_STORED_DIGEST_ROUNDS); |
| 597 | std::string serialized_string[MAX_PASSWORDS]; |
| 598 | Digest_info digest_type = Digest_info::SHA256_DIGEST; |
| 599 | std::string digest; |
| 600 | std::string salt; |
| 601 | std::string plaintext; |
| 602 | size_t iterations = 5000; |
| 603 | |
| 604 | /* Part 1 : Populate the cache */ |
| 605 | |
| 606 | std::string auth_id_arthur("'arthur'@'dent.com'"); |
| 607 | const char plaintext_buffer_arthur[] = "HahaH0hO1234#$@#%"; |
| 608 | const char salt_buffer_arthur[] = "AbCd!@# EfgH%^&*01@#"; |
| 609 | const char digest_buffer_arthur[] = { |
| 610 | 0x64, 0x4e, 0x46, 0x65, 0x67, 0x45, 0x61, 0x34, 0x4f, 0x47, 0x44, |
| 611 | 0x72, 0x6e, 0x45, 0x75, 0x37, 0x4d, 0x34, 0x54, 0x2e, 0x6e, 0x6b, |
| 612 | 0x6d, 0x6d, 0x37, 0x39, 0x34, 0x78, 0x49, 0x2f, 0x51, 0x6f, 0x43, |
| 613 | 0x52, 0x33, 0x5a, 0x2e, 0x78, 0x70, 0x4c, 0x4f, 0x57, 0x36}; |
| 614 | digest.assign(digest_buffer_arthur, STORED_SHA256_DIGEST_LENGTH); |
| 615 | salt.assign(salt_buffer_arthur); |
| 616 | plaintext.assign(plaintext_buffer_arthur); |
| 617 | ASSERT_TRUE(caching_sha2_password.serialize(serialized_string[0], digest_type, |
| 618 | salt, digest, |
| 619 | iterations) == false); |
| 620 | ASSERT_TRUE(caching_sha2_password |
| 621 | .authenticate(auth_id_arthur, serialized_string, plaintext) |
| 622 | .first == false); |
| 623 | |
| 624 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 1); |
| 625 | // Attempt again, it should pass |
| 626 | ASSERT_TRUE(caching_sha2_password |
| 627 | .authenticate(auth_id_arthur, serialized_string, plaintext) |
| 628 | .first == false); |
| 629 | |
| 630 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 1); |
| 631 | // Attempt with incorrect digest, it should fail |
| 632 | const char digest_buffer_arthur_ic[] = { |
| 633 | 0x64, 0x4e, 0x46, 0x65, 0x67, 0x45, 0x61, 0x34, 0x4f, 0x47, 0x44, |
| 634 | 0x72, 0x6e, 0x45, 0x75, 0x37, 0x4d, 0x34, 0x54, 0x2e, 0x6e, 0x6b, |
| 635 | 0x6d, 0x6d, 0x37, 0x39, 0x34, 0x78, 0x49, 0x2f, 0x51, 0x6f, 0x43, |
| 636 | 0x52, 0x33, 0x5a, 0x2e, 0x78, 0x70, 0x4c, 0x4f, 0x57, 0x3a}; |
| 637 | |
| 638 | digest.assign(digest_buffer_arthur_ic, STORED_SHA256_DIGEST_LENGTH); |
| 639 | ASSERT_TRUE(caching_sha2_password.serialize(serialized_string[0], digest_type, |
| 640 | salt, digest, |
| 641 | iterations) == false); |
| 642 | ASSERT_TRUE(caching_sha2_password |
| 643 | .authenticate(auth_id_arthur, serialized_string, plaintext) |
| 644 | .first == true); |
| 645 | |
| 646 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 1); |
| 647 | std::string auth_id_marvin("'marvin'@'theparanoidandroid.com'"); |
| 648 | const char plaintext_buffer_marvin[] = ";bCdEF34^i9&*\":({\\56\""; |
| 649 | const char salt_buffer_marvin[] = "CVlq))+AC>Q)#4!@# x!"; |
| 650 | const char digest_buffer_marvin[] = { |
| 651 | 0x53, 0x50, 0x4d, 0x4e, 0x2f, 0x4c, 0x38, 0x2e, 0x6e, 0x6f, 0x4b, |
| 652 | 0x71, 0x65, 0x38, 0x49, 0x58, 0x56, 0x5a, 0x32, 0x44, 0x70, 0x43, |
| 653 | 0x45, 0x36, 0x77, 0x32, 0x39, 0x62, 0x31, 0x30, 0x36, 0x76, 0x43, |
| 654 | 0x51, 0x65, 0x63, 0x61, 0x70, 0x39, 0x4a, 0x4b, 0x70, 0x41}; |
| 655 | |
| 656 | digest.assign(digest_buffer_marvin, STORED_SHA256_DIGEST_LENGTH); |
| 657 | salt.assign(salt_buffer_marvin); |
| 658 | plaintext.assign(plaintext_buffer_marvin); |
| 659 | |
| 660 | ASSERT_TRUE(caching_sha2_password.serialize(serialized_string[0], digest_type, |
| 661 | salt, digest, |
| 662 | iterations) == false); |
| 663 | ASSERT_TRUE(caching_sha2_password |
| 664 | .authenticate(auth_id_marvin, serialized_string, plaintext) |
| 665 | .first == false); |
| 666 | // Attempt again, it should pass |
| 667 | ASSERT_TRUE(caching_sha2_password |
| 668 | .authenticate(auth_id_marvin, serialized_string, plaintext) |
| 669 | .first == false); |
| 670 | |
| 671 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 2); |
| 672 | std::string auth_id_zaphod("'zaphod'@'beeblebrox'"); |
| 673 | const char plaintext_buffer_zaphod[] = " CQ#$ML CF%IF$#(<#R ()<_q@#(rq"; |
| 674 | const char salt_buffer_zaphod[] = "X#y)+q x2cx3,qr2##3 "; |
| 675 | const char digest_buffer_zaphod[] = { |
| 676 | 0x72, 0x50, 0x38, 0x4a, 0x6e, 0x38, 0x4d, 0x74, 0x70, 0x36, 0x58, |
| 677 | 0x43, 0x4d, 0x78, 0x31, 0x51, 0x6a, 0x53, 0x44, 0x41, 0x38, 0x4e, |
| 678 | 0x53, 0x61, 0x6c, 0x66, 0x56, 0x4d, 0x53, 0x42, 0x4e, 0x4d, 0x49, |
| 679 | 0x72, 0x32, 0x79, 0x71, 0x63, 0x45, 0x69, 0x36, 0x51, 0x38}; |
| 680 | |
| 681 | digest.assign(digest_buffer_zaphod, STORED_SHA256_DIGEST_LENGTH); |
| 682 | salt.assign(salt_buffer_zaphod); |
| 683 | plaintext.assign(plaintext_buffer_zaphod); |
| 684 | |
| 685 | ASSERT_TRUE(caching_sha2_password.serialize(serialized_string[0], digest_type, |
| 686 | salt, digest, |
| 687 | iterations) == false); |
| 688 | ASSERT_TRUE(caching_sha2_password |
| 689 | .authenticate(auth_id_zaphod, serialized_string, plaintext) |
| 690 | .first == false); |
| 691 | // Attempt again, it should pass |
| 692 | ASSERT_TRUE(caching_sha2_password |
| 693 | .authenticate(auth_id_zaphod, serialized_string, plaintext) |
| 694 | .first == false); |
| 695 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 3); |
| 696 | |
| 697 | /* Part 2 : Perform fast authentication */ |
| 698 | std::string scramble_random_zaphod("CVOS)=M@)=*%!)#_[-(2"); |
| 699 | Generate_scramble generate_scramble(plaintext, scramble_random_zaphod); |
| 700 | unsigned char scramble_zaphod[CACHING_SHA2_DIGEST_LENGTH]; |
| 701 | generate_scramble.scramble(scramble_zaphod, CACHING_SHA2_DIGEST_LENGTH); |
| 702 | |
| 703 | ASSERT_TRUE(caching_sha2_password |
| 704 | .fast_authenticate(auth_id_zaphod, |
| 705 | reinterpret_cast<const unsigned char *>( |
| 706 | scramble_random_zaphod.c_str()), |
| 707 | scramble_random_zaphod.length(), |
| 708 | scramble_zaphod, false) |
| 709 | .first == false); |
| 710 | caching_sha2_password.remove_cached_entry(auth_id_zaphod); |
| 711 | ASSERT_TRUE(caching_sha2_password |
| 712 | .fast_authenticate(auth_id_zaphod, |
| 713 | reinterpret_cast<const unsigned char *>( |
| 714 | scramble_random_zaphod.c_str()), |
| 715 | scramble_random_zaphod.length(), |
| 716 | scramble_zaphod, false) |
| 717 | .first == true); |
| 718 | |
| 719 | plaintext.assign(plaintext_buffer_arthur); |
| 720 | char random_salt_buffer[CRYPT_SALT_LENGTH + 1]; |
| 721 | generate_user_salt(random_salt_buffer, CRYPT_SALT_LENGTH + 1); |
| 722 | std::string scramble_random_arthur(random_salt_buffer); |
| 723 | Generate_scramble generate_scramble_arthur(plaintext, scramble_random_arthur); |
| 724 | unsigned char scramble_arthur[CACHING_SHA2_DIGEST_LENGTH]; |
| 725 | generate_scramble_arthur.scramble(scramble_arthur, |
| 726 | CACHING_SHA2_DIGEST_LENGTH); |
| 727 | |
| 728 | ASSERT_TRUE(caching_sha2_password |
| 729 | .fast_authenticate(auth_id_arthur, |
| 730 | reinterpret_cast<const unsigned char *>( |
| 731 | scramble_random_arthur.c_str()), |
| 732 | scramble_random_arthur.length(), |
| 733 | scramble_arthur, false) |
| 734 | .first == false); |
| 735 | |
| 736 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 2); |
| 737 | caching_sha2_password.clear_cache(); |
| 738 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 0); |
| 739 | } |
| 740 | |
| 741 | TEST_F(SHA256_digestTest, Caching_sha2_password_authenticate_sanity) { |
| 742 | Caching_sha2_password caching_sha2_password(nullptr, |
| 743 | DEFAULT_STORED_DIGEST_ROUNDS); |
| 744 | std::string serialized_string[MAX_PASSWORDS]; |
| 745 | std::string plaintext; |
| 746 | |
| 747 | std::string auth_id_arthur("'arthur'@'dent.com'"); |
| 748 | const char empty_plaintext_buffer_arthur[] = ""; |
| 749 | const char nonempty_plaintext_buffer_arthur[] = "HahaH0hO1234#$@#%"; |
| 750 | const char invalid_plaintext_buffer_arthur[] = |
| 751 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 752 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 753 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 754 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 755 | |
| 756 | plaintext.assign(empty_plaintext_buffer_arthur); |
| 757 | ASSERT_TRUE(caching_sha2_password |
| 758 | .authenticate(auth_id_arthur, serialized_string, plaintext) |
| 759 | .first == false); |
| 760 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 0); |
| 761 | |
| 762 | plaintext.assign(nonempty_plaintext_buffer_arthur); |
| 763 | ASSERT_TRUE(caching_sha2_password |
| 764 | .authenticate(auth_id_arthur, serialized_string, plaintext) |
| 765 | .first == true); |
| 766 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 0); |
| 767 | |
| 768 | plaintext.assign(invalid_plaintext_buffer_arthur); |
| 769 | ASSERT_TRUE(caching_sha2_password |
| 770 | .authenticate(auth_id_arthur, serialized_string, plaintext) |
| 771 | .first == true); |
| 772 | ASSERT_TRUE(caching_sha2_password.get_cache_count() == 0); |
| 773 | } |
| 774 | } // namespace sha2_password_unittest |