emberemu/tests/GameIntegrity.cpp

98 lines
No EOL
4.3 KiB
C++

/*
* Copyright (c) 2024 - 2026 Ember
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <login/ExecutablesChecksum.h>
#include <shared/utility/HashDefines.h>
#include <gtest/gtest.h>
#include <array>
#include <vector>
#include <cstdint>
using namespace ember;
const std::array<std::uint8_t, 256> sample_buffer {
0x38, 0x62, 0x96, 0x78, 0xB2, 0xEF, 0x28, 0xA6, 0xA9, 0xB0, 0xC8, 0x45, 0x38, 0xF2, 0x1F, 0xCE,
0xE8, 0x48, 0xBC, 0xC2, 0x03, 0xAF, 0x2D, 0x79, 0xB8, 0x4A, 0x5F, 0xDD, 0x7A, 0xA5, 0x9A, 0x24,
0x2F, 0x77, 0xD5, 0xDC, 0xCA, 0x0F, 0xB2, 0xE3, 0x1D, 0xDE, 0x71, 0x35, 0x09, 0x70, 0x68, 0xF4,
0x38, 0x6D, 0x16, 0x3A, 0x2B, 0x04, 0x38, 0x52, 0x62, 0x9A, 0xCF, 0x66, 0x36, 0x6D, 0x3D, 0xBE,
0x7C, 0x32, 0x20, 0x80, 0x16, 0x18, 0xB8, 0xCF, 0x7E, 0x5D, 0x1B, 0xC8, 0x3D, 0xDA, 0xAB, 0x81,
0xEB, 0xEB, 0x52, 0x52, 0x91, 0x61, 0x09, 0x70, 0x56, 0xEC, 0x06, 0xDD, 0x90, 0x59, 0x86, 0x72,
0x1A, 0x60, 0x03, 0xA0, 0xEC, 0x8D, 0x9F, 0x67, 0xD3, 0x49, 0x0B, 0xE7, 0x76, 0xDA, 0x58, 0x75,
0xF9, 0x31, 0x07, 0xD6, 0xCD, 0xF9, 0xBB, 0x99, 0xF1, 0x8A, 0xC5, 0xED, 0xF2, 0xE3, 0x88, 0xA7,
0xF6, 0x9A, 0x3A, 0xCA, 0x2C, 0x4D, 0x4F, 0x2B, 0xD3, 0xA4, 0x94, 0x93, 0xE0, 0xB3, 0x36, 0xE9,
0xB1, 0x5B, 0x81, 0xB0, 0x1D, 0x6C, 0x3F, 0x78, 0x7C, 0xE1, 0x8C, 0x5A, 0xC8, 0x3C, 0xCF, 0xD0,
0xE7, 0x74, 0x17, 0xE7, 0x67, 0x07, 0x82, 0xBC, 0x17, 0xD9, 0xF8, 0xF4, 0x2C, 0x7B, 0x80, 0x9D,
0xB0, 0x05, 0x5B, 0xC7, 0x84, 0x8A, 0xD2, 0x93, 0xF0, 0x6E, 0xFA, 0x99, 0x0A, 0x95, 0x91, 0x05,
0xD1, 0xE7, 0xA8, 0xB9, 0x4B, 0x34, 0xD2, 0xBC, 0x0D, 0x5C, 0x78, 0xC2, 0xEC, 0x64, 0xA2, 0x95,
0x35, 0x7D, 0x28, 0x66, 0xBD, 0x96, 0x0C, 0xD2, 0x7A, 0x32, 0xFB, 0x72, 0xB1, 0xCB, 0x80, 0x2A,
0x4A, 0xF4, 0xFF, 0x5C, 0x44, 0xA7, 0x0F, 0xC0, 0x84, 0x5B, 0xFF, 0xCE, 0x0F, 0x0F, 0x3A, 0x45,
0x68, 0x2B, 0xAF, 0x00, 0x83, 0xAE, 0xE5, 0x3E, 0x65, 0x1F, 0x18, 0xF6, 0xB6, 0x9A, 0x0B, 0xEE,
};
TEST(GameIntegrity, FileIntegrity) {
const std::array<std::uint8_t, hash_sizes::md5> seed {
0x1a, 0x61, 0x68, 0xfd, 0xf7, 0xba, 0x9b, 0xdb,
0xeb, 0x86, 0x21, 0x67, 0xd, 0x7f, 0xbd, 0x9f
};
const std::array<std::uint8_t, 32> salt {
0x88, 0xf7, 0x9e, 0xab, 0x68, 0xe7, 0x1e, 0xc9,
0xe2, 0xf4, 0xe4, 0x51, 0x66, 0x39, 0xdc, 0x5d,
0x1d, 0x30, 0x00, 0xad, 0x15, 0xb0, 0xb4, 0xba,
0x1d, 0x58, 0xf7, 0x3b, 0x58, 0xd1, 0xd2, 0x73
};
const std::array<std::uint8_t, hash_sizes::sha160> expected_bin_cs {
0xaa, 0x42, 0x93, 0xf1, 0xec, 0x44, 0x5d, 0x5f, 0x3a, 0x7e,
0x45, 0x44, 0x63, 0x06, 0x1a, 0xae, 0x86, 0xad, 0x8f, 0xc5
};
const std::span buffer(sample_buffer);
const auto bin_checksum = client_integrity::checksum(seed, std::as_bytes(buffer));
ASSERT_EQ(bin_checksum, expected_bin_cs);
const std::array<std::uint8_t, hash_sizes::sha160> expected_checksum {
0x33, 0x6e, 0x8f, 0xaf, 0xe1, 0xea, 0x92, 0x3c, 0x99, 0x54,
0xa8, 0xcd, 0x94, 0x69, 0xe5, 0x04, 0xff, 0xfc, 0xe6, 0x08
};
const auto checksum = client_integrity::finalise(bin_checksum, salt);
ASSERT_EQ(checksum, expected_checksum);
}
TEST(GameIntegrity, GameResponse) {
// This was the salt input to client_integrity::checksum but
// can't include the data that's used as the second argument in
// a unit test committed to a repo :)
const std::array<std::uint8_t, 16> seed {
0x1a, 0x61, 0x68, 0xfd, 0xf7, 0xba, 0x9b, 0xdb,
0xeb, 0x86, 0x21, 0x67, 0xd, 0x7f, 0xbd, 0x9f
};
(void)seed; // just to stop the compiler warning about the above unused var
const std::array<std::uint8_t, hash_sizes::sha160> sha1_hmac {
0xa5, 0x32, 0x7c, 0x48, 0xe4, 0xf7, 0x77, 0xb8, 0x4e, 0xa,
0xf0, 0x38, 0x68, 0x3f, 0xfa, 0x33, 0x18, 0xdf, 0x12, 0xa8
};
const std::array<std::uint8_t, 32> client_salt_A {
0x88, 0xf7, 0x9e, 0xab, 0x68, 0xe7, 0x1e, 0xc9,
0xe2, 0xf4, 0xe4, 0x51, 0x66, 0x39, 0xdc, 0x5d,
0x1d, 0x30, 0x00, 0xad, 0x15, 0xb0, 0xb4, 0xba,
0x1d, 0x58, 0xf7, 0x3b, 0x58, 0xd1, 0xd2, 0x73
};
const std::array<std::uint8_t, hash_sizes::sha160> expected_checksum {
0x16, 0xea, 0x06, 0xf7, 0xd7, 0x75, 0xde, 0x25, 0xa2, 0x0e,
0x7c, 0x54, 0x1d, 0xca, 0xa1, 0xe9, 0xf7, 0x18, 0xa0, 0x34
};
const auto checksum = client_integrity::finalise(sha1_hmac, client_salt_A);
ASSERT_EQ(checksum, expected_checksum);
}