Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
*.dll
*.exe
*.o
Expand Down
84 changes: 84 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
4 changes: 2 additions & 2 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ../..
2 changes: 2 additions & 0 deletions th135arc-alt/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.pak
*.o
th135/
th155/
th135arc-alt
th135arc-alt.exe
1 change: 1 addition & 0 deletions th135arc-alt/FnList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# include <map>
# include <vector>
# include <filesystem>
#include <algorithm>
# include "OS.hpp"
# include "Rsa.hpp"

Expand Down
1 change: 0 additions & 1 deletion th135arc-alt/MIRACL
Submodule MIRACL deleted from ad618f
4 changes: 2 additions & 2 deletions th135arc-alt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
103 changes: 46 additions & 57 deletions th135arc-alt/Rsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
#include <vector>
#include <string.h>
#include "Rsa.hpp"
#include <iostream>

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<std::array<unsigned char, KEY_BYTESIZE> > KEYS_N = {
// Touhou 13.5
Expand Down Expand Up @@ -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)
Expand All @@ -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.
**
Expand Down Expand Up @@ -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<unsigned char> 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)
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 9 additions & 11 deletions th135arc-alt/Rsa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
# define RSA_HPP_

# include <fstream>

extern "C"
{
# include "miracl.h"
}
#include "cryptopp/rsa.h"
#include "cryptopp/osrng.h"
#include "cryptopp/integer.h"

class Rsa
{
Expand All @@ -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);
Expand All @@ -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_ */
1 change: 0 additions & 1 deletion th145arc/TFPKArchive.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef TFPKARCHIVE_H_
# define TFPKARCHIVE_H_
# include <Windows.h>

#pragma pack(push)
#pragma pack(1)
Expand Down