-
Notifications
You must be signed in to change notification settings - Fork 121
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
Windows 7 support? Missing ProcessPrng
#1997
Comments
Hello! Thanks for letting us know about this problem, and for the reference to how Rust solved it. I was trying to reproduce this with aws-lc-rs, but I'm having trouble with the build. I suspect my issue relates to not having the toolchain setup. (Since this is a tier 3 platform, are there any pre-built toolchains available?) Here's what I tried (on a Windows host using the bash shell provided by Git), and the resulting error:
If you can provide some guidance on how to setup a development environment for this, it would be appreciated. Thanks! |
Hey, I appreciate you being open to supporting a legacy system like Windows 7. For my initial test project (shown above) I also used [dependencies]
aws-lc-rs = { version = "1.11.0", default-features = false, features = ["aws-lc-sys", "prebuilt-nasm"] } If I reference [dependencies]
aws-lc-rs = { path = "../aws-lc-rs/aws-lc-rs", default-features = false, features = ["aws-lc-sys", "prebuilt-nasm"] } Do you have an idea what might cause this error? Errors building test project with direct
|
This looks like the error I get when I forget to initialize the git submodules. (The Makefile in our project has some convenient targets for doing this.) Thanks for the response. I'll take a closer look at this tomorrow. |
This is part of rustls's performance CI infrastructure, and is not intended to be portable to platforms other than linux. |
Thanks, I somehow missed that. Using my test project with The previous code in this repo used The patch that I've useddiff --git a/crypto/rand_extra/windows.c b/crypto/rand_extra/windows.c
--- a/crypto/rand_extra/windows.c (revision 745359e8569fdafa8897ac2fffdfd0fdcf620563)
+++ b/crypto/rand_extra/windows.c (date 1732146659715)
@@ -24,6 +24,11 @@
OPENSSL_MSVC_PRAGMA(warning(push, 3))
#include <windows.h>
+#include <bcrypt.h>
+
+#ifndef NT_SUCCESS
+#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
+#endif
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \
!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
@@ -56,17 +61,44 @@
#else
-// See: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng
-typedef BOOL (WINAPI *ProcessPrngFunction)(PBYTE pbData, SIZE_T cbData);
-static ProcessPrngFunction g_processprng_fn = NULL;
+typedef NTSTATUS (WINAPI *BCryptGenRandomFunction)(
+_In_opt_ BCRYPT_ALG_HANDLE hAlgorithm,
+_Out_writes_bytes_(cbBuffer) PUCHAR pbBuffer,
+_In_ ULONG cbBuffer,
+_In_ ULONG dwFlags
+);
+static BCryptGenRandomFunction g_BCryptGenRandom_fn = NULL;
+static BCRYPT_ALG_HANDLE g_bcrypt_algorithm = NULL;
static void init_processprng(void) {
- HMODULE hmod = LoadLibraryW(L"bcryptprimitives");
- if (hmod == NULL) {
+ HMODULE hmod = LoadLibraryW(L"bcrypt.dll");
+ if (hmod == NULL)
+ {
abort();
}
- g_processprng_fn = (ProcessPrngFunction)(void(*)(void))GetProcAddress(hmod, "ProcessPrng");
- if (g_processprng_fn == NULL) {
+
+ g_BCryptGenRandom_fn = (BCryptGenRandomFunction)GetProcAddress(hmod, "BCryptGenRandom");
+ if (g_BCryptGenRandom_fn == NULL)
+ {
+ abort();
+ }
+
+ typedef NTSTATUS (WINAPI *BCryptOpenAlgorithmProviderFunction)(
+ _Out_ BCRYPT_ALG_HANDLE *phAlgorithm,
+ _In_z_ LPCWSTR pszAlgId,
+ _In_opt_z_ LPCWSTR pszImplementation,
+ _In_ ULONG dwFlags
+ );
+
+ BCryptOpenAlgorithmProviderFunction algoFunc = (BCryptOpenAlgorithmProviderFunction)GetProcAddress(hmod, "BCryptOpenAlgorithmProvider");
+ if (algoFunc == NULL)
+ {
+ abort();
+ }
+
+ NTSTATUS status = algoFunc(&g_bcrypt_algorithm, BCRYPT_RNG_ALGORITHM, NULL, 0);
+ if (!NT_SUCCESS(status))
+ {
abort();
}
}
@@ -78,11 +110,11 @@
void CRYPTO_sysrand(uint8_t *out, size_t requested) {
CRYPTO_init_sysrand();
- // On non-UWP configurations, use ProcessPrng instead of BCryptGenRandom
- // to avoid accessing resources that may be unavailable inside the
- // Chromium sandbox. See https://crbug.com/74242
- if (!g_processprng_fn(out, requested)) {
- abort();
+
+ NTSTATUS status = g_BCryptGenRandom_fn(g_bcrypt_algorithm, out, (ULONG)requested, 0);
+ if (!NT_SUCCESS(status))
+ {
+ abort();
}
} Maybe it's possible to clean this up a bit and put it behind a conditional |
Thanks for the patch! Since this involves very security sensitive logic (i.e., entropy/randomness), it will take some time for us to consider it. As you indicated, in the end this change will likely be guarded by an |
Could one decide the code-path at compile-time? That is, if on Windows 7, then specifically compile the Maybe this
I don't have a Win 7 on hand. Can you check if you have a test system set up already? |
Problem:
I actually came here from aws-lc-rust. I tried to use the following minimal example with
x86_64-win7-windows-msvc
but as soon as the library tries to accessProcessPrng
the program aborts.Erroneous line:
aws-lc/crypto/rand_extra/windows.c
Line 68 in 745359e
Solution:
Rust recently switched from
BCryptGenRandom
toProcessPrng
but allowed a path for Windows 7.rust-lang/rust#121337
Is a similar solution also possible in this project?
The text was updated successfully, but these errors were encountered: