Skip to content

prng: support getrandom & getentropy#1367

Merged
achernya merged 1 commit intomobile-shell:masterfrom
vapier:random
Feb 28, 2026
Merged

prng: support getrandom & getentropy#1367
achernya merged 1 commit intomobile-shell:masterfrom
vapier:random

Conversation

@vapier
Copy link
Copy Markdown
Contributor

@vapier vapier commented Jan 20, 2026

If the C library supports these random functions, use them directly instead of reading the /dev/urandom file. This makes life easier on platforms that don't have /dev/urandom (like WASM).

@achernya
Copy link
Copy Markdown
Collaborator

What platforms have arc4random but not getrandom? I'd rather not add support for arc4 which has known weaknesses

@vapier
Copy link
Copy Markdown
Contributor Author

vapier commented Jan 20, 2026

WASI (standardized WASM interface) doesn't have getrandom() unfortunately. it has getentropy().

i can swap the define preference so getrandom() comes first.

@achernya
Copy link
Copy Markdown
Collaborator

Preferring getrandom over arc4random sounds like a good move -- how does that help with getentropy, though?

@vapier
Copy link
Copy Markdown
Contributor Author

vapier commented Jan 20, 2026

Preferring getrandom over arc4random sounds like a good move

done

how does that help with getentropy, though?

it's orthogonal of course. I was just noting that WASI supports arc4random & getentropy, but not getrandom. I can drop arc4random & add getentropy if you want as it still makes things work for me (just tested).

@achernya
Copy link
Copy Markdown
Collaborator

If that works, that sounds preferable. getrandom and getentropy are both reasonable modern APIs.

Copy link
Copy Markdown

@Lillecarl Lillecarl left a comment

Choose a reason for hiding this comment

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

Works on my machine

@vapier
Copy link
Copy Markdown
Contributor Author

vapier commented Jan 20, 2026

ok, it'll take me a little bit to adjust to getentropy in the general case. the documented API says it operates on max size of 256, and while all the calls in mosh production code are well below that, src/tests/encrypt-decrypt.cc grabs a couple KB :).

@vapier
Copy link
Copy Markdown
Contributor Author

vapier commented Jan 20, 2026

okidoki, this version supports getrandom first, then getentropy, then /dev/urandom. while the getentropy code is annoying due to the loop, a spot check on x86_64 shows it's only a few bytes larger than getrandom when compiled with -O2.

Comment thread src/crypto/prng.h
@vapier vapier changed the title prng: support getrandom & arc4random_buf prng: support getrandom & getentropy Jan 20, 2026
Comment thread src/crypto/prng.h
Comment thread src/crypto/prng.h Outdated
@achernya
Copy link
Copy Markdown
Collaborator

If you rebase this we should be able to run CI on it and get this merged.

@achernya
Copy link
Copy Markdown
Collaborator

Can you please apply the clang-format fixes from https://github.com/mobile-shell/mosh/actions/runs/22505470885/job/65202884828?pr=1367

If the C library supports these random functions, use them directly
instead of reading the /dev/urandom file.  This makes life easier on
platforms that don't have /dev/urandom (like WASM).
@vapier
Copy link
Copy Markdown
Contributor Author

vapier commented Feb 28, 2026

ran clang-format -i -style=file on it

@achernya achernya merged commit b5775df into mobile-shell:master Feb 28, 2026
6 checks passed
@vapier vapier deleted the random branch March 30, 2026 04:36
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