diff --git a/.gitignore b/.gitignore index 733c555..3c4cfdb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.vscode *.dll *.exe *.o diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9b7c848 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,84 @@ +{ + "files.associations": { + "any": "cpp", + "array": "cpp", + "atomic": "cpp", + "hash_map": "cpp", + "bit": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "codecvt": "cpp", + "compare": "cpp", + "complex": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "coroutine": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "source_location": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "format": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "queue": "cpp", + "ranges": "cpp", + "semaphore": "cpp", + "span": "cpp", + "sstream": "cpp", + "stack": "cpp", + "stdexcept": "cpp", + "stdfloat": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "text_encoding": "cpp", + "thread": "cpp", + "cfenv": "cpp", + "typeinfo": "cpp", + "variant": "cpp" + } +} \ No newline at end of file diff --git a/init.sh b/init.sh index 5fd5b8c..9c73e78 100755 --- a/init.sh +++ b/init.sh @@ -4,11 +4,11 @@ git submodule update cd th135arc-alt/MIRACL zip -r miracl.zip * unzip -j -aa -L -o miracl.zip -bash linux +bash linux64 cd ../.. cd th145arc/MIRACL zip -r miracl.zip * unzip -j -aa -L -o miracl.zip -bash linux +bash linux64 cd ../.. diff --git a/th135arc-alt/.gitignore b/th135arc-alt/.gitignore index 45c8f83..7aa54d2 100644 --- a/th135arc-alt/.gitignore +++ b/th135arc-alt/.gitignore @@ -1,4 +1,6 @@ *.pak *.o +th135/ +th155/ th135arc-alt th135arc-alt.exe diff --git a/th135arc-alt/FnList.hpp b/th135arc-alt/FnList.hpp index 40439df..996b165 100644 --- a/th135arc-alt/FnList.hpp +++ b/th135arc-alt/FnList.hpp @@ -4,6 +4,7 @@ # include # include # include +#include # include "OS.hpp" # include "Rsa.hpp" diff --git a/th135arc-alt/MIRACL b/th135arc-alt/MIRACL deleted file mode 160000 index ad618fe..0000000 --- a/th135arc-alt/MIRACL +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ad618fedb997a6bc70f7898802b60e831c8d8ca6 diff --git a/th135arc-alt/Makefile b/th135arc-alt/Makefile index 87293a5..19fd062 100644 --- a/th135arc-alt/Makefile +++ b/th135arc-alt/Makefile @@ -22,9 +22,9 @@ CXXFLAGS += -municode LDFLAGS += -municode endif -CXXFLAGS += -IMIRACL +# CXXFLAGS += -I./cryptopp/ -LDFLAGS += MIRACL/miracl.a -lz -g +LDFLAGS += -lcryptopp -lz -g all: $(NAME) diff --git a/th135arc-alt/Rsa.cpp b/th135arc-alt/Rsa.cpp index 612809e..b06df10 100644 --- a/th135arc-alt/Rsa.cpp +++ b/th135arc-alt/Rsa.cpp @@ -2,11 +2,14 @@ #include #include #include "Rsa.hpp" +#include + +using namespace CryptoPP; static const int KEY_SIZE = 512; // in bits static const int KEY_BYTESIZE = KEY_SIZE / 8; -bool Rsa::miraclInitialized = false; +bool Rsa::initialized = false; static const std::vector > KEYS_N = { // Touhou 13.5 @@ -73,17 +76,14 @@ static const unsigned char KEY_d[KEY_BYTESIZE] = { static const unsigned char PaddingBytes[32] = {0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00}; -void Rsa::initMiracl() +void Rsa::initCryptoPP() { - miracl* mip = mirsys(128,16); - mip->IOBASE = 16; - Rsa::miraclInitialized = true; + Rsa::initialized = true; } -void Rsa::freeMiracl() +void Rsa::freeCryptoPP() { - mirexit(); - Rsa::miraclInitialized = false; + Rsa::initialized = false; } Rsa::Rsa(std::ifstream& file) @@ -100,46 +100,43 @@ Rsa::Rsa(std::ofstream& file, Rsa::CryptMode cryptMode) void Rsa::init() { - if (!Rsa::miraclInitialized) - Rsa::initMiracl(); - - RSA_e = mirvar(0x10001); - RSA_d = mirvar(0); - bytes_to_big(KEY_BYTESIZE, (const char*)KEY_d, RSA_d); + if (!Rsa::initialized) + Rsa::initCryptoPP(); + + RSA_e = CryptoPP::Integer(0x10001); + RSA_d = CryptoPP::Integer(); + RSA_N = CryptoPP::Integer(); + + if (ifile) { + unsigned char crypted_sample[64]; + ifile->read((char*)crypted_sample, 64); + if (!this->initRsaPublicKey(crypted_sample)) { + std::cerr << "No matching RSA key found.\n"; + } - // For encryption, take the Touhou 13.5 public key (TODO: add support for other keys). - // For decryption, initRsaPublicKey will be called later. - RSA_N = mirvar(0); - bytes_to_big(KEY_BYTESIZE, (const char*)KEYS_N[0].data(), RSA_N); + ifile->seekg(-64, std::ios::cur); + } } Rsa::~Rsa() { - mirkill(RSA_e); - mirkill(RSA_d); - mirkill(RSA_N); } -bool Rsa::initRsaPublicKey(const unsigned char *crypted_sample) +bool Rsa::initRsaPublicKey(const unsigned char* crypted_sample) { - // Guess the correct key for decryption - for (auto& it : KEYS_N) - { - mirkill(RSA_N); - RSA_N = mirvar(0); - bytes_to_big(KEY_BYTESIZE, (const char*)it.data(), RSA_N); + for (const auto& it : KEYS_N) + { + this->RSA_N = CryptoPP::Integer(it.data(), it.size()); - unsigned char tmp[64] = {0}; - this->DecryptBlock(crypted_sample, tmp); - if (this->checkPadding(tmp)) - { - this->publicKeyInitialized = true; - return true; - } + unsigned char tmp[64] = {0}; + this->DecryptBlock(crypted_sample, tmp); + if (this->checkPadding(tmp)) { + this->publicKeyInitialized = true; + return true; } + } return false; } - /* ** This comment may be wrong, but that's how I understand things. ** @@ -186,30 +183,22 @@ bool Rsa::skipPaddingAndCopy(const unsigned char *src, unsigned char *dst, size_ return true; } -void Rsa::DecryptBlock(const unsigned char *src, unsigned char *dst) +void Rsa::DecryptBlock(const unsigned char* src, unsigned char* dst) { - big sint = mirvar(0); - bytes_to_big(KEY_BYTESIZE, (const char*)src, sint); - - big result = mirvar(0); - powmod(sint, RSA_e, RSA_N, result); - big_to_bytes(KEY_BYTESIZE, result, (char*)dst, TRUE); - - mirkill(sint); - mirkill(result); + CryptoPP::Integer c(src, 64); + CryptoPP::Integer m = a_exp_b_mod_c(c, RSA_e, RSA_N); + CryptoPP::ArraySink as(dst, 64); + m.Encode(as, 64); } -void Rsa::EncryptBlock(const unsigned char* src, unsigned char* dst) -{ - big sint = mirvar(0); - bytes_to_big(KEY_BYTESIZE,(const char*)src, sint); - - big result = mirvar(0); - powmod(sint, RSA_d, RSA_N, result); - big_to_bytes(KEY_BYTESIZE, result, (char*)dst, TRUE); +void Rsa::EncryptBlock(const unsigned char* src, unsigned char* dst) { + Integer m(src, KEY_BYTESIZE); + Integer c = a_exp_b_mod_c(m, RSA_e, RSA_N); + size_t count = c.MinEncodedSize(); + std::vector temp(KEY_BYTESIZE, 0); - mirkill(sint); - mirkill(result); + c.Encode(temp.data() + (KEY_BYTESIZE - count), count); + std::move(temp.begin(), temp.end(), dst); } bool Rsa::Decrypt6432(const unsigned char* src, unsigned char* dst, size_t dstLen) @@ -270,7 +259,7 @@ bool Rsa::read32(void *buffer, size_t size) bool Rsa::read(void *buffer, size_t size) { if (!this->ifile) - throw std::logic_error("Calling Rsa::read on a stream in outputj mode"); + throw std::logic_error("Calling Rsa::read on a stream in output mode"); char *buffer_ = (char*)buffer; while (size > 32) { diff --git a/th135arc-alt/Rsa.hpp b/th135arc-alt/Rsa.hpp index 41f2470..518f98c 100644 --- a/th135arc-alt/Rsa.hpp +++ b/th135arc-alt/Rsa.hpp @@ -2,11 +2,9 @@ # define RSA_HPP_ # include - -extern "C" -{ -# include "miracl.h" -} +#include "cryptopp/rsa.h" +#include "cryptopp/osrng.h" +#include "cryptopp/integer.h" class Rsa { @@ -17,15 +15,15 @@ class Rsa }; private: - static bool miraclInitialized; + static bool initialized; std::ifstream* ifile; std::ofstream* ofile; bool publicKeyInitialized = false; CryptMode cryptMode; - big RSA_N; - big RSA_d; - big RSA_e; + CryptoPP::Integer RSA_N; + CryptoPP::Integer RSA_d; + CryptoPP::Integer RSA_e; void init(); bool initRsaPublicKey(const unsigned char* crypted_sample); @@ -47,8 +45,8 @@ class Rsa bool read(void *buffer, size_t size); bool write(const void *buffer, size_t size); - static void initMiracl(); - static void freeMiracl(); + static void initCryptoPP(); + static void freeCryptoPP(); }; #endif /* RSA_HPP_ */ diff --git a/th145arc/TFPKArchive.h b/th145arc/TFPKArchive.h index 162bc1f..d58bdf3 100644 --- a/th145arc/TFPKArchive.h +++ b/th145arc/TFPKArchive.h @@ -1,6 +1,5 @@ #ifndef TFPKARCHIVE_H_ # define TFPKARCHIVE_H_ -# include #pragma pack(push) #pragma pack(1)