| author | |
| committer | |
| log | 9ecbabfc4ce857e43db2b056bc83272cb24b0bbd |
| tree | 6f5e3a6c93f6e0859819f0866e7c37a7ea9a4637 |
| parent | 432b7685bfa840a459b492a37894f7ffed870c7e |
| parent | 729f2aceb045e54b2b74a33fa5f64cb9802988c6 |
3 files changed, 18 insertions(+), 13 deletions(-)
std/os/index.zig+12-12| ... | @@ -130,16 +130,10 @@ pub fn getRandomBytes(buf: []u8) !void { | ... | @@ -130,16 +130,10 @@ pub fn getRandomBytes(buf: []u8) !void { |
| 130 | try posixRead(fd, buf); | 130 | try posixRead(fd, buf); |
| 131 | }, | 131 | }, |
| 132 | Os.windows => { | 132 | Os.windows => { |
| 133 | var hCryptProv: windows.HCRYPTPROV = undefined; | 133 | // Call RtlGenRandom() instead of CryptGetRandom() on Windows |
| 134 | if (windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0) == 0) { | 134 | // https://github.com/rust-lang-nursery/rand/issues/111 |
| 135 | const err = windows.GetLastError(); | 135 | // https://bugzilla.mozilla.org/show_bug.cgi?id=504270 |
| 136 | return switch (err) { | 136 | if (windows.RtlGenRandom(buf.ptr, buf.len) == 0) { |
| 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) { | ||
| 143 | const err = windows.GetLastError(); | 137 | const err = windows.GetLastError(); |
| 144 | return switch (err) { | 138 | return switch (err) { |
| 145 | else => unexpectedErrorWindows(err), | 139 | else => unexpectedErrorWindows(err), |
| ... | @@ -159,8 +153,14 @@ pub fn getRandomBytes(buf: []u8) !void { | ... | @@ -159,8 +153,14 @@ pub fn getRandomBytes(buf: []u8) !void { |
| 159 | } | 153 | } |
| 160 | 154 | ||
| 161 | test "os.getRandomBytes" { | 155 | test "os.getRandomBytes" { |
| 162 | var buf: [50]u8 = undefined; | 156 | var buf_a: [50]u8 = undefined; |
| 163 | try getRandomBytes(buf[0..]); | 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) ); | ||
| 164 | } | 164 | } |
| 165 | 165 | ||
| 166 | /// Raises a signal in the current kernel thread, ending its execution. | 166 | /// 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, | ... | @@ -28,3 +28,8 @@ pub extern "advapi32" stdcallcc fn RegOpenKeyExW(hKey: HKEY, lpSubKey: LPCWSTR, |
| 28 | 28 | ||
| 29 | pub extern "advapi32" stdcallcc fn RegQueryValueExW(hKey: HKEY, lpValueName: LPCWSTR, lpReserved: LPDWORD, | 29 | pub extern "advapi32" stdcallcc fn RegQueryValueExW(hKey: HKEY, lpValueName: LPCWSTR, lpReserved: LPDWORD, |
| 30 | lpType: LPDWORD, lpData: LPBYTE, lpcbData: LPDWORD,) LSTATUS; | 30 | 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 */ | ||
| 34 | pub extern "advapi32" stdcallcc fn SystemFunction036(output: [*]u8, length: usize) BOOL; | ||
| 35 | pub const RtlGenRandom = SystemFunction036; |
std/os/windows/util.zig+1-1| ... | @@ -166,7 +166,7 @@ pub fn windowsUnloadDll(hModule: windows.HMODULE) void { | ... | @@ -166,7 +166,7 @@ pub fn windowsUnloadDll(hModule: windows.HMODULE) void { |
| 166 | } | 166 | } |
| 167 | 167 | ||
| 168 | test "InvalidDll" { | 168 | test "InvalidDll" { |
| 169 | if (builtin.os != builtin.Os.windows) return; | 169 | if (builtin.os != builtin.Os.windows) return error.SkipZigTest; |
| 170 | 170 | ||
| 171 | const DllName = "asdf.dll"; | 171 | const DllName = "asdf.dll"; |
| 172 | const allocator = std.debug.global_allocator; | 172 | const allocator = std.debug.global_allocator; |