authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-02 13:35:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-02 13:35:06-04:00
log9ecbabfc4ce857e43db2b056bc83272cb24b0bbd
tree6f5e3a6c93f6e0859819f0866e7c37a7ea9a4637
parent432b7685bfa840a459b492a37894f7ffed870c7e
parent729f2aceb045e54b2b74a33fa5f64cb9802988c6

Merge branch 'pr-1319'


3 files changed, 18 insertions(+), 13 deletions(-)

std/os/index.zig+12-12
......@@ -130,16 +130,10 @@ pub fn getRandomBytes(buf: []u8) !void {
130130 try posixRead(fd, buf);
131131 },
132132 Os.windows => {
133 var hCryptProv: windows.HCRYPTPROV = undefined;
134 if (windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0) == 0) {
135 const err = windows.GetLastError();
136 return switch (err) {
137 else => unexpectedErrorWindows(err),
138 };
139 }
140 defer _ = windows.CryptReleaseContext(hCryptProv, 0);
141
142 if (windows.CryptGenRandom(hCryptProv, @intCast(windows.DWORD, buf.len), buf.ptr) == 0) {
133 // Call RtlGenRandom() instead of CryptGetRandom() on Windows
134 // https://github.com/rust-lang-nursery/rand/issues/111
135 // https://bugzilla.mozilla.org/show_bug.cgi?id=504270
136 if (windows.RtlGenRandom(buf.ptr, buf.len) == 0) {
143137 const err = windows.GetLastError();
144138 return switch (err) {
145139 else => unexpectedErrorWindows(err),
......@@ -159,8 +153,14 @@ pub fn getRandomBytes(buf: []u8) !void {
159153}
160154
161155test "os.getRandomBytes" {
162 var buf: [50]u8 = undefined;
163 try getRandomBytes(buf[0..]);
156 var buf_a: [50]u8 = undefined;
157 var buf_b: [50]u8 = undefined;
158 // Call Twice
159 try getRandomBytes(buf_a[0..]);
160 try getRandomBytes(buf_b[0..]);
161
162 // Check if random (not 100% conclusive)
163 assert( !mem.eql(u8, buf_a, buf_b) );
164164}
165165
166166/// Raises a signal in the current kernel thread, ending its execution.
std/os/windows/advapi32.zig+5
......@@ -28,3 +28,8 @@ pub extern "advapi32" stdcallcc fn RegOpenKeyExW(hKey: HKEY, lpSubKey: LPCWSTR,
2828
2929pub extern "advapi32" stdcallcc fn RegQueryValueExW(hKey: HKEY, lpValueName: LPCWSTR, lpReserved: LPDWORD,
3030 lpType: LPDWORD, lpData: LPBYTE, lpcbData: LPDWORD,) LSTATUS;
31
32// RtlGenRandom is known as SystemFunction036 under advapi32
33// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */
34pub extern "advapi32" stdcallcc fn SystemFunction036(output: [*]u8, length: usize) BOOL;
35pub const RtlGenRandom = SystemFunction036;
std/os/windows/util.zig+1-1
......@@ -166,7 +166,7 @@ pub fn windowsUnloadDll(hModule: windows.HMODULE) void {
166166}
167167
168168test "InvalidDll" {
169 if (builtin.os != builtin.Os.windows) return;
169 if (builtin.os != builtin.Os.windows) return error.SkipZigTest;
170170
171171 const DllName = "asdf.dll";
172172 const allocator = std.debug.global_allocator;