Skip to content

Use CCRandomGenerateBytes on macOS - #879

Merged
newpavlov merged 5 commits into
rust-random:masterfrom
patrick-immometrica:macos-ccrandom
Nov 17, 2026
Merged

newpavlov merged 5 commits into
rust-random:masterfrom
patrick-immometrica:macos-ccrandom

Conversation

@patrick-immometrica

Copy link
Copy Markdown
Contributor

On macOS we call getentropy, which is a syscall per call and capped at 256 bytes. This switches macOS to CCRandomGenerateBytes, which the iOS/tvOS/watchOS backend already uses and which std uses on all Apple targets (library/std/src/sys/random/apple.rs). It's available since macOS 10.10, so the 10.12 minimum is fine.

cargo +nightly bench on an M5 MacBook, master vs this branch:

bench master branch
u32 551 ns 63 ns
u64 553 ns 62 ns
fill 32 bytes (p256) 760 ns 186 ns
fill 4096 bytes (page) 11.5 µs 0.88 µs

#823 discussed getentropy vs. the CommonCrypto API from the entropy-quality side. This one is just about speed, and even seed-sized requests get ~4x faster.

I also dropped macOS from the __error arm in get_errno.rs since nothing on macOS uses it anymore, and updated the README table and the nopanic step names.

@patrick-immometrica
patrick-immometrica marked this pull request as ready for review November 15, 2026 18:15

@newpavlov newpavlov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I fixed the CI failure in #876.

Comment thread .github/workflows/nopanic.yaml Resolved
Comment thread Cargo.toml Resolved

@josephlr josephlr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, we had different implementations on macOS and iOS/tvOS/etc... due to the cost of linking in the Security Framework for SecRandomCopyBytes. However, #388 (correctly) switched all our iOS/etc... implementations to use CCRandomGenerateBytes.

As that PR notes, CCRandomGenerateBytes is part of libSystem, it's always linked in (it contains the libc symbols for example), so that cost no longer exists.

I think changing the macOS implementation is reasonable, it will simplify things w.r.t. Common Criteria and it aligns us with what Python does.

Note that BoringSSL still uses getentropy on macOS, I'm not sure why, but my guess is keeping the implementation similar between macOS and OpenBSD.

Comment thread src/backends/apple.rs Resolved
Comment thread .github/workflows/nopanic.yaml Resolved
@patrick-immometrica

Copy link
Copy Markdown
Contributor Author

Thanks! Renamed the backend to apple.rs and dropped the iOS step from the nopanic job, so it now only runs Build (apple.rs) on aarch64-apple-darwin. The iOS clippy step in workspace.yml is still there, just renamed.

@josephlr josephlr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased this onto master, and made a few minor changes (renamed error constant to APPLE_RANDOM_GEN and fixed some links in the docs and names in the CI). Looks good to merge.

I'll let @newpavlov confirm that my changes are good.

Signed-off-by: Joe Richey <joerichey@google.com>
Signed-off-by: Joe Richey <joerichey@google.com>
@newpavlov
newpavlov merged commit 97a7d49 into rust-random:master Nov 17, 2026
82 checks passed
@newpavlov newpavlov mentioned this pull request Nov 17, 2026
@newpavlov

Copy link
Copy Markdown
Member

Huh... What a lucky timing for Nightly to introduce extern_item_default_impls unstable feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants