windows: use ProcessPrng from bcryptprimitives.dll #30688

Closed
andrewrk wants to merge 7 commits from ProcessPrng into master
Owner

builds on #30686

rather than SystemFunction036 from advapi32. This has the advantage that
the code is loaded preemptively, preventing random numbers from
failing when they are needed for the first time on a system under heavy
load.

related: https://github.com/ziglang/zig/issues/23666

builds on https://codeberg.org/ziglang/zig/pulls/30686 rather than SystemFunction036 from advapi32. This has the advantage that the code is loaded preemptively, preventing random numbers from failing when they are needed for the first time on a system under heavy load. related: https://github.com/ziglang/zig/issues/23666
Apparently the O_TMPFILE flag is split across two bits on these
architectures and missing on m68k.
test-standalone: explicitly link to advapi32 sometimes
Some checks failed
ci / x86_64-freebsd-release (pull_request) Successful in 35m10s
ci / x86_64-freebsd-debug (pull_request) Successful in 57m14s
ci / aarch64-macos-release (pull_request) Successful in 1h0m1s
ci / x86_64-linux-debug (pull_request) Successful in 1h13m28s
ci / aarch64-linux-release (pull_request) Successful in 1h30m27s
ci / x86_64-linux-release (pull_request) Successful in 1h57m36s
ci / aarch64-macos-debug (pull_request) Successful in 1h58m54s
ci / aarch64-linux-debug (pull_request) Successful in 2h32m25s
ci / s390x-linux-release (pull_request) Successful in 1h37m55s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h42m38s
ci / s390x-linux-debug (pull_request) Successful in 4h7m30s
ci / loongarch64-linux-debug (pull_request) Failing after 2h50m20s
ci / loongarch64-linux-release (pull_request) Failing after 3h0m37s
ci / x86_64-windows-debug (pull_request) Failing after 48m38s
ci / x86_64-windows-release (pull_request) Failing after 55m31s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
36044a0e81
Now, nearly every object produced by zig has a dependency on
advapi32.dll due to the dependency on RtlGenRandom due to the
implementation of dirCreateFileAtomic which is now analyzed whether or
not it is used.
windows: use ProcessPrng from bcryptprimitives.dll
Some checks failed
ci / x86_64-windows-release (pull_request) Failing after 6m42s
ci / x86_64-windows-debug (pull_request) Failing after 8m30s
ci / x86_64-freebsd-release (pull_request) Failing after 33m52s
ci / x86_64-freebsd-debug (pull_request) Failing after 47m24s
ci / aarch64-macos-release (pull_request) Failing after 57m50s
ci / aarch64-linux-release (pull_request) Failing after 1h13m45s
ci / x86_64-linux-debug (pull_request) Failing after 1h22m18s
ci / x86_64-linux-debug-llvm (pull_request) Failing after 1h37m20s
ci / aarch64-macos-debug (pull_request) Failing after 1h54m40s
ci / aarch64-linux-debug (pull_request) Failing after 1h58m22s
ci / x86_64-linux-release (pull_request) Failing after 2h9m52s
ci / s390x-linux-debug (pull_request) Failing after 2h44m49s
ci / s390x-linux-release (pull_request) Failing after 1h32m48s
ci / loongarch64-linux-release (pull_request) Failing after 1h38m23s
ci / loongarch64-linux-debug (pull_request) Failing after 2h20m28s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
60e2ea0bfb
rather than SystemFunction036 from advapi32. This has the advantage that
the code is loaded preemptively, preventing random numbers from
failing when they are needed for the first time on a system under heavy
load.
Owner
diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig
index c041293e2f..07c9dd22ca 100644
--- a/lib/std/os/windows.zig
+++ b/lib/std/os/windows.zig
@@ -1080,6 +1080,9 @@ pub const CTL_CODE = packed struct(ULONG) {
};

pub const IOCTL = struct {
+    pub const KSEC = struct {
+        pub const GEN_RANDOM: CTL_CODE = .{ .DeviceType = .KSEC, .Function = 2, .Method = .BUFFERED, .Access = .ANY };
+    };
    pub const MOUNTMGR = struct {
        pub const QUERY_POINTS: CTL_CODE = .{ .DeviceType = .MOUNTMGRCONTROLTYPE, .Function = 2, .Method = .BUFFERED, .Access = .ANY };
        pub const QUERY_DOS_VOLUME_PATH: CTL_CODE = .{ .DeviceType = .MOUNTMGRCONTROLTYPE, .Function = 12, .Method = .BUFFERED, .Access = .ANY };
const std = @import("std");
pub fn main() void {
    const cng_path = "\\Device\\CNG";
    var cng_device: std.os.windows.HANDLE = undefined;
    var iosb: std.os.windows.IO_STATUS_BLOCK = undefined;
    switch (std.os.windows.ntdll.NtOpenFile(
        &cng_device,
        .{
            .STANDARD = .{ .SYNCHRONIZE = true },
            .SPECIFIC = .{ .FILE = .{ .READ_DATA = true } },
        },
        &.{
            .Length = @sizeOf(std.os.windows.OBJECT_ATTRIBUTES),
            .RootDirectory = null,
            .ObjectName = @constCast(&std.os.windows.UNICODE_STRING{
                .Length = cng_path.len * @sizeOf(std.os.windows.WCHAR),
                .MaximumLength = 0,
                .Buffer = @constCast(std.unicode.wtf8ToWtf16LeStringLiteral(cng_path)),
            }),
            .Attributes = .{},
            .SecurityDescriptor = null,
            .SecurityQualityOfService = null,
        },
        &iosb,
        .VALID_FLAGS,
        .{ .IO = .SYNCHRONOUS_NONALERT },
    )) {
        .SUCCESS => {},
        else => |err| @panic(@tagName(err)),
    }
    defer _ = std.os.windows.ntdll.NtClose(cng_device);
    var buffer: [0x30]u8 = undefined;
    switch (std.os.windows.ntdll.NtDeviceIoControlFile(
        cng_device,
        null,
        null,
        null,
        &iosb,
        std.os.windows.IOCTL.KSEC.GEN_RANDOM,
        null,
        0,
        &buffer,
        buffer.len,
    )) {
        .SUCCESS => {},
        else => |err| @panic(@tagName(err)),
    }
    std.debug.dumpHex(&buffer);
}

My impression is that all ProcessPrng is doing is lazily initializing one AES-CTR-DRBG PRNG per cpu, each seeded from a shared per-process PRNG, which is seeded at startup from CNG.

```diff diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index c041293e2f..07c9dd22ca 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -1080,6 +1080,9 @@ pub const CTL_CODE = packed struct(ULONG) { }; pub const IOCTL = struct { + pub const KSEC = struct { + pub const GEN_RANDOM: CTL_CODE = .{ .DeviceType = .KSEC, .Function = 2, .Method = .BUFFERED, .Access = .ANY }; + }; pub const MOUNTMGR = struct { pub const QUERY_POINTS: CTL_CODE = .{ .DeviceType = .MOUNTMGRCONTROLTYPE, .Function = 2, .Method = .BUFFERED, .Access = .ANY }; pub const QUERY_DOS_VOLUME_PATH: CTL_CODE = .{ .DeviceType = .MOUNTMGRCONTROLTYPE, .Function = 12, .Method = .BUFFERED, .Access = .ANY }; ``` ```zig const std = @import("std"); pub fn main() void { const cng_path = "\\Device\\CNG"; var cng_device: std.os.windows.HANDLE = undefined; var iosb: std.os.windows.IO_STATUS_BLOCK = undefined; switch (std.os.windows.ntdll.NtOpenFile( &cng_device, .{ .STANDARD = .{ .SYNCHRONIZE = true }, .SPECIFIC = .{ .FILE = .{ .READ_DATA = true } }, }, &.{ .Length = @sizeOf(std.os.windows.OBJECT_ATTRIBUTES), .RootDirectory = null, .ObjectName = @constCast(&std.os.windows.UNICODE_STRING{ .Length = cng_path.len * @sizeOf(std.os.windows.WCHAR), .MaximumLength = 0, .Buffer = @constCast(std.unicode.wtf8ToWtf16LeStringLiteral(cng_path)), }), .Attributes = .{}, .SecurityDescriptor = null, .SecurityQualityOfService = null, }, &iosb, .VALID_FLAGS, .{ .IO = .SYNCHRONOUS_NONALERT }, )) { .SUCCESS => {}, else => |err| @panic(@tagName(err)), } defer _ = std.os.windows.ntdll.NtClose(cng_device); var buffer: [0x30]u8 = undefined; switch (std.os.windows.ntdll.NtDeviceIoControlFile( cng_device, null, null, null, &iosb, std.os.windows.IOCTL.KSEC.GEN_RANDOM, null, 0, &buffer, buffer.len, )) { .SUCCESS => {}, else => |err| @panic(@tagName(err)), } std.debug.dumpHex(&buffer); } ``` My impression is that all `ProcessPrng` is doing is lazily initializing one `AES-CTR-DRBG` PRNG per cpu, each seeded from a shared per-process PRNG, which is seeded at startup from `CNG`.
Author
Owner

Nice find!

Nice find!
Author
Owner
https://codeberg.org/ziglang/zig/pulls/30703
andrewrk closed this pull request 2026-01-05 21:18:34 +01:00
First-time contributor

short question to @jacobly: Where did you find this code?

short question to @jacobly: Where did you find this code?
Some checks are pending
ci / x86_64-windows-release (pull_request) Failing after 6m42s
Required
Details
ci / x86_64-windows-debug (pull_request) Failing after 8m30s
Required
Details
ci / x86_64-freebsd-release (pull_request) Failing after 33m52s
Required
Details
ci / x86_64-freebsd-debug (pull_request) Failing after 47m24s
Required
Details
ci / aarch64-macos-release (pull_request) Failing after 57m50s
Required
Details
ci / aarch64-linux-release (pull_request) Failing after 1h13m45s
Required
Details
ci / x86_64-linux-debug (pull_request) Failing after 1h22m18s
Required
Details
ci / x86_64-linux-debug-llvm (pull_request) Failing after 1h37m20s
Required
Details
ci / aarch64-macos-debug (pull_request) Failing after 1h54m40s
Required
Details
ci / aarch64-linux-debug (pull_request) Failing after 1h58m22s
Required
Details
ci / x86_64-linux-release (pull_request) Failing after 2h9m52s
Required
Details
ci / s390x-linux-debug (pull_request) Failing after 2h44m49s
Required
Details
ci / s390x-linux-release (pull_request) Failing after 1h32m48s
Required
Details
ci / loongarch64-linux-release (pull_request) Failing after 1h38m23s
Required
Details
ci / loongarch64-linux-debug (pull_request) Failing after 2h20m28s
Required
Details
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / aarch64-freebsd-debug (pull_request)
Required
ci / aarch64-freebsd-release (pull_request)
Required
ci / aarch64-netbsd-debug (pull_request)
Required
ci / aarch64-netbsd-release (pull_request)
Required
ci / aarch64-openbsd-debug (pull_request)
Required
ci / aarch64-openbsd-release (pull_request)
Required
ci / powerpc64le-linux-debug (pull_request)
Required
ci / powerpc64le-linux-release (pull_request)
Required
ci / x86_64-netbsd-debug (pull_request)
Required
ci / x86_64-netbsd-release (pull_request)
Required
ci / x86_64-openbsd-debug (pull_request)
Required
ci / x86_64-openbsd-release (pull_request)
Required

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ziglang/zig!30688
No description provided.