Skip to content

Add Windows support #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ add_library(LLVMObfuscator SHARED Plugin.cpp)
target_include_directories(LLVMObfuscator PRIVATE ${CMAKE_SOURCE_DIR})
target_include_directories(LLVMObfuscator PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

target_link_libraries(LLVMObfuscator LLVMCore LLVMSupport)
target_link_libraries(LLVMObfuscator LLVMCore LLVMSupport LLVMAnalysis LLVMTransformUtils)

option(BUILD_DUMMY "Build dummy plugin" OFF)
if(BUILD_DUMMY)
Expand Down
66 changes: 60 additions & 6 deletions utils/CryptoUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,26 +610,80 @@ void CryptoUtils::populate_pool() {
idx = 0;
}

#if defined(_WIN64) || defined(_WIN32)
// sic! don't change include order
#include <windows.h>
#include <wincrypt.h>

struct WinDevRandom {
WinDevRandom() : m_hcryptProv{0}, m_last_read{0} {
assert(!m_hcryptProv);
if (!CryptAcquireContext(&m_hcryptProv, nullptr, nullptr, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
errs() << "CryptAcquireContext failed (LastError: " << GetLastError()
<< ")\n";
} else {
assert(m_hcryptProv);
}
}

~WinDevRandom() { close(); }

std::size_t read(char *key, std::size_t sz) {
assert(m_hcryptProv);
if (!CryptGenRandom(m_hcryptProv, sz, reinterpret_cast<BYTE *>(key))) {
errs() << "CryptGenRandom failed (LastError: " << GetLastError() << ")\n";
}
m_last_read = sz;
return sz;
}

void close() {
if (m_hcryptProv && !CryptReleaseContext(m_hcryptProv, 0)) {
errs() << "CryptReleaseContext failed (LastError: " << GetLastError()
<< ")\n";
}
m_hcryptProv = 0;
assert(!m_hcryptProv);
}

std::size_t gcount() { return m_last_read; }

explicit operator bool() { return true; }

bool good() const { return m_hcryptProv; }

private:
HCRYPTPROV m_hcryptProv;
std::size_t m_last_read;
};
#endif

bool CryptoUtils::prng_seed() {

#if defined(__linux__)
std::ifstream devrandom("/dev/urandom");
std::string const dev = "/dev/urandom";
std::ifstream devrandom(dev);
#elif defined(_WIN64) || defined(_WIN32)
std::string const dev = "CryptGenRandom";
WinDevRandom devrandom;
#else
std::ifstream devrandom("/dev/random");
std::string const dev = "/dev/random";
std::ifstream devrandom(dev);
#endif

if (devrandom) {
if (devrandom.good()) {

devrandom.read(key, 16);

if (devrandom.gcount() != 16) {
errs() << "Cannot read enough bytes in /dev/random\n";
errs() << "Cannot read enough bytes in " << dev << "\n";
return false;
}

devrandom.close();
DEBUG_WITH_TYPE("cryptoutils",
dbgs() << "cryptoutils seeded with /dev/random\n");
dbgs() << "cryptoutils seeded with " << dev << "\n");

memset(ctr, 0, 16);

Expand All @@ -639,7 +693,7 @@ bool CryptoUtils::prng_seed() {

seeded = true;
} else {
errs() << "Cannot open /dev/random\n";
errs() << "Cannot open " << dev << "\n";
return false;
}
return true;
Expand Down
7 changes: 6 additions & 1 deletion utils/CryptoUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ extern ManagedStatic<CryptoUtils> cryptoutils;
#define BYTE(x, n) (((x) >> (8 * (n))) & 0xFF)

#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
defined(INTEL_CC)
defined(INTEL_CC) || defined(_WIN64) || defined(_WIN32)

#ifndef ENDIAN_LITTLE
#define ENDIAN_LITTLE
#endif
#define ENDIAN_32BITWORD

#if !defined(_WIN64) || !defined(_WIN32)
#ifndef UNALIGNED
#define UNALIGNED
#endif
#endif

#elif defined(__alpha)

Expand Down