Skip to content

Commit 2e66935

Browse files
committed
Merge remote-tracking branch 'upstream/llvm-3.4' into llvm-3.4
2 parents bd0482c + a6eccc3 commit 2e66935

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

lib/Transforms/Obfuscation/PrngAESCtr.cpp

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
#include <cstring>
3333
#include <cstdio>
3434

35+
#if defined(_WIN32)
36+
#include <windows.h>
37+
#include <wincrypt.h>
38+
#endif
39+
3540
// Stats
3641
STATISTIC(statsGetBytes, "a. Number of calls to get_bytes ()");
3742
STATISTIC(statsGetChar, "b. Number of calls to get_char ()");
@@ -747,35 +752,51 @@ void PrngAESCtr::populate_pool () {
747752
}
748753

749754
void PrngAESCtr::prng_seed () {
750-
#if defined(__linux__)
751-
std::ifstream devrandom ("/dev/urandom");
755+
756+
LLVMContext &ctx = llvm::getGlobalContext();
757+
758+
#if defined(_WIN32)
759+
HCRYPTPROV hProvider = 0;
760+
761+
if (!::CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
762+
ctx.emitError(Twine("Cannot acquire cryptographic context"));
763+
}
764+
765+
if (!::CryptGenRandom(hProvider, 16, (unsigned char*)key))
766+
{
767+
::CryptReleaseContext(hProvider, 0);
768+
ctx.emitError(Twine("Cannot generate random"));
769+
}
770+
771+
if (!::CryptReleaseContext(hProvider, 0)) {
772+
ctx.emitError(Twine("Cannot release cryptographic context"));
773+
}
774+
775+
DEBUG_WITH_TYPE("cprng", dbgs() << "CPNRG seeded with Windows cryptographic API");
752776
#else
753-
std::ifstream devrandom ("/dev/random");
754-
#endif
755-
756-
LLVMContext &ctx = llvm::getGlobalContext();
757-
777+
std::ifstream devrandom ("/dev/urandom");
778+
758779
if (devrandom) {
759780

760781
devrandom.read (key, 16);
761782

762783
if (devrandom.gcount() != 16) {
763-
ctx.emitError ( Twine("Cannot read enough bytes in /dev/random"));
784+
ctx.emitError ( Twine("Cannot read enough bytes in /dev/urandom"));
764785
}
765786

766787
devrandom.close();
767-
DEBUG_WITH_TYPE ("cprng", dbgs() << "CPNRG seeded with /dev/random");
768-
769-
memset(ctr, 0, 16);
770-
771-
// Once the seed is there, we compute the
772-
// AES128 key-schedule
773-
aes_compute_ks(ks, key);
774-
775-
seeded = true;
776788
} else {
777-
ctx.emitError (Twine ("Cannot open /dev/random"));
789+
ctx.emitError (Twine ("Cannot open /dev/urandom"));
778790
}
791+
#endif
792+
793+
memset(ctr, 0, 16);
794+
795+
// Once the seed is there, we compute the
796+
// AES128 key-schedule
797+
aes_compute_ks(ks, key);
798+
799+
seeded = true;
779800
}
780801

781802
void PrngAESCtr::inc_ctr() {

tools/clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ def mios_simulator_version_min_EQ : Joined<["-"], "mios-simulator-version-min=">
973973
def mkernel : Flag<["-"], "mkernel">, Group<m_Group>;
974974
def mlinker_version_EQ : Joined<["-"], "mlinker-version=">,
975975
Flags<[DriverOption]>;
976-
def mllvm : Separate<["-"], "mllvm">, Flags<[CC1Option]>,
976+
def mllvm : Separate<["-"], "mllvm">, Flags<[DriverOption,CC1Option]>,
977977
HelpText<"Additional arguments to forward to LLVM's option processing">;
978978
def mmacosx_version_min_EQ : Joined<["-"], "mmacosx-version-min=">, Group<m_Group>;
979979
def mms_bitfields : Flag<["-"], "mms-bitfields">, Group<m_Group>, Flags<[CC1Option]>,

0 commit comments

Comments
 (0)