From 22fd359e2c601c9d0a009705bed60c88821f2b0f Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Fri, 3 Aug 2018 02:14:06 +0900 Subject: std/os/windows/advapi32.zig: add SystemFunction036; Tracking Issue #1318 ; --- std/os/windows/advapi32.zig | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'std') diff --git a/std/os/windows/advapi32.zig b/std/os/windows/advapi32.zig index dcb5a636ea..64a820a3b1 100644 --- a/std/os/windows/advapi32.zig +++ b/std/os/windows/advapi32.zig @@ -28,3 +28,8 @@ pub extern "advapi32" stdcallcc fn RegOpenKeyExW(hKey: HKEY, lpSubKey: LPCWSTR, pub extern "advapi32" stdcallcc fn RegQueryValueExW(hKey: HKEY, lpValueName: LPCWSTR, lpReserved: LPDWORD, lpType: LPDWORD, lpData: LPBYTE, lpcbData: LPDWORD,) LSTATUS; + +// RtlGenRandom is known as SystemFunction036 under advapi32 +// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */ +pub extern "advapi32" stdcallcc fn SystemFunction036(output: PVOID, length: ULONG_PTR) BOOL; +pub const RtlGenRandom = SystemFunction036; -- cgit v1.2.3 From c44653f40f37c93fc68a2e455d693bb11f1c11d3 Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Fri, 3 Aug 2018 02:14:52 +0900 Subject: std/os/index.zig: swap CryptGetRandom() with RtlGenRandom(); Tracking Issue #1318 ; --- std/os/index.zig | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'std') diff --git a/std/os/index.zig b/std/os/index.zig index 77fd2a78ad..7042247a2f 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -130,16 +130,10 @@ pub fn getRandomBytes(buf: []u8) !void { try posixRead(fd, buf); }, Os.windows => { - var hCryptProv: windows.HCRYPTPROV = undefined; - if (windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0) == 0) { - const err = windows.GetLastError(); - return switch (err) { - else => unexpectedErrorWindows(err), - }; - } - defer _ = windows.CryptReleaseContext(hCryptProv, 0); - - if (windows.CryptGenRandom(hCryptProv, @intCast(windows.DWORD, buf.len), buf.ptr) == 0) { + // Call RtlGenRandom() instead of CryptGetRandom() on Windows + // https://github.com/rust-lang-nursery/rand/issues/111 + // https://bugzilla.mozilla.org/show_bug.cgi?id=504270 + if (!windows.RtlGenRandom(buf.ptr, buf.len)) { const err = windows.GetLastError(); return switch (err) { else => unexpectedErrorWindows(err), -- cgit v1.2.3 From dde7eb45c51dd96242a6b028eb1912ee6ffa2c55 Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Fri, 3 Aug 2018 02:16:19 +0900 Subject: std/os/index.zig: call getRandomBytes() twice and compare; Tracking Issue #1318 ; --- std/os/index.zig | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'std') diff --git a/std/os/index.zig b/std/os/index.zig index 7042247a2f..87edce3dac 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -153,8 +153,14 @@ pub fn getRandomBytes(buf: []u8) !void { } test "os.getRandomBytes" { - var buf: [50]u8 = undefined; - try getRandomBytes(buf[0..]); + var buf_a: [50]u8 = undefined; + var buf_b: [50]u8 = undefined; + // Call Twice + try getRandomBytes(buf_a[0..]); + try getRandomBytes(buf_b[0..]); + + // Check if random (not 100% conclusive) + assert( !mem.eql(u8, buf_a, buf_b) ); } /// Raises a signal in the current kernel thread, ending its execution. -- cgit v1.2.3 From 782043e2e66ab5833cb20dc6e787b87eb84165f8 Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Fri, 3 Aug 2018 02:16:49 +0900 Subject: std/os/windows/util.zig: SKIP instead of PASS on non-windows systems; Tracking Issue #1318 ; --- std/os/windows/util.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'std') diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig index dda9ce7a8b..c9d2c3c3e6 100644 --- a/std/os/windows/util.zig +++ b/std/os/windows/util.zig @@ -166,7 +166,7 @@ pub fn windowsUnloadDll(hModule: windows.HMODULE) void { } test "InvalidDll" { - if (builtin.os != builtin.Os.windows) return; + if (builtin.os != builtin.Os.windows) return error.SkipZigTest; const DllName = "asdf.dll"; const allocator = std.debug.global_allocator; -- cgit v1.2.3 From 729f2aceb045e54b2b74a33fa5f64cb9802988c6 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 2 Aug 2018 13:34:31 -0400 Subject: fix API of RtlGenRandom --- std/os/index.zig | 2 +- std/os/windows/advapi32.zig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'std') diff --git a/std/os/index.zig b/std/os/index.zig index b5e0129da4..425a900a71 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -133,7 +133,7 @@ pub fn getRandomBytes(buf: []u8) !void { // Call RtlGenRandom() instead of CryptGetRandom() on Windows // https://github.com/rust-lang-nursery/rand/issues/111 // https://bugzilla.mozilla.org/show_bug.cgi?id=504270 - if (!windows.RtlGenRandom(buf.ptr, buf.len)) { + if (windows.RtlGenRandom(buf.ptr, buf.len) == 0) { const err = windows.GetLastError(); return switch (err) { else => unexpectedErrorWindows(err), diff --git a/std/os/windows/advapi32.zig b/std/os/windows/advapi32.zig index 64a820a3b1..2f3195475c 100644 --- a/std/os/windows/advapi32.zig +++ b/std/os/windows/advapi32.zig @@ -31,5 +31,5 @@ pub extern "advapi32" stdcallcc fn RegQueryValueExW(hKey: HKEY, lpValueName: LPC // RtlGenRandom is known as SystemFunction036 under advapi32 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */ -pub extern "advapi32" stdcallcc fn SystemFunction036(output: PVOID, length: ULONG_PTR) BOOL; +pub extern "advapi32" stdcallcc fn SystemFunction036(output: [*]u8, length: usize) BOOL; pub const RtlGenRandom = SystemFunction036; -- cgit v1.2.3