8000 Add CryptoRngProvider trait by wez · Pull Request #4 · mkj/sunset · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add CryptoRngProvider trait #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Add CryptoRngProvider trait #4

wants to merge 2 commits into from

Conversation

wez
Copy link
Contributor
@wez wez commented Apr 13, 2025

This makes it a bit easier to plug in an RNG when not building on a platform that has an OsRng.

wez added 2 commits April 12, 2025 21:27
This commit defines an interface that allows an embedding
application to set an extension trait which can process
the functions that require a CryptoRng.

It does this without infecting the type signature of
essentially every part of sunset.

The implementation is 100% safe when building with the default getrandom
feature enabled, but when that feature is omitted and the new assignment
interface is added, there is some unsafe code to manage replacing the
pointer to that extension trait.

This is not likely totally sound in every scenario, but it happens
to work for me.

There's probably some cribbing that could be done from the log::logger()
and log::set_logger_racy() functions to make this more-safe.
It resolves to 'a anyway, so let's just set it to that
@mkj
Copy link
Owner
mkj commented Apr 14, 2025

Thanks for the patch. I'm not quite sure whether it makes sense setting the RNG here or using getrandom/custom feature. What kind of platform are you targetting? For the picow example I'm using a custom RNG with getrandom, that seemed to work OK? (that's still getrandom 0.2, it looks like 0.3 would be similar, though maybe a bit more messy needing to work with raw pointers [1]).

// RNG initialised early, all crypto relies on it
caprand::setup(&mut p.PIN_10).unwrap();
getrandom::register_custom_getrandom!(caprand::getrandom);

Using getrandom has the advantage that it'll hopefully work with any other crypto-consuming parts of an application too (ongoing discussion of something similar in std or core [2]). That said maybe the approach in this PR makes sense - happy to hear your thoughts.

[1] https://docs.rs/getrandom/0.3.2/getrandom/index.html#custom-backend
[2] rust-lang/rust#130703 (comment)

@wez
Copy link
Contributor Author
wez commented Apr 15, 2025

Oh, I somehow missed that getrandom had its own extension mechanism. Yeah, I can hook that up.
The reason I went down this path was because OsRng was defined in a different crate and was not supported, but looking at it with fresh eyes, I can see that it uses getrandom, so that's a double comprehension fail on my part :-/
In my defense, I got lost in a ton of tabs and examples.

I'm running on an rp2350 which actually has hardware TRNG available.

https://fosstodon.org/@wez/114325695508602149 shows sunset working on a picocalc on a pico 2w!

@wez
Copy link
Contributor Author
wez commented Apr 15, 2025

Ah, the other issue is that getrandom 0.2 doesn't compile for this platform:

error: target is not supported, for more information see: https://docs.rs/getrandom/#unsupported-targets
   --> /Users/wez/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.2.15/src/lib.rs:347:9
    |
347 | /         compile_error!("target is not supported, for more information see: \
348 | |                         https://docs.rs/getrandom/#unsupported-targets");
    | |________________________________________________________________________^

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `imp`
   --> /Users/wez/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/getrandom-0.2.15/src/lib.rs:398:9
    |
398 |         imp::getrandom_inner(dest)?;
    |         ^^^ use of unresolved module or unlinked crate `imp`
    |
    = help: if you wanted to use a crate named `imp`, use `cargo add imp` to add it to your `Cargo.toml`

I think 0.3 gets further along, but there are some other dependencies to run down, and I'm not sure if those are solvable with the current version of embassy-rp. I'll dig in a bit more.

@mkj
Copy link
Owner
mkj commented Apr 15, 2025

Ah awesome with the picocalc. I'd been eyeing those off, maybe I should get one!
Let me know if you have any ideas/questions with it. Better terminal emulation is something I was wondering about...

Ah, the other issue is that getrandom 0.2 doesn't compile for this platform:

It works if you build with getrandom/custom feature.

getrandom = { version = "0.2", features = ["custom"] }

I'll have a look how 0.3 goes when I get a chance.

@wez
Copy link
Contributor Author
wez commented Apr 15, 2025

I looked at 0.3, but it's blocked on dalek-cryptography/curve25519-dalek#729 which is incomplete right now; I tried using a git ref to that in sunset, but it's not quite there.

I did confirm just now that 0.2 with the custom feature works!

What I'll do is amend this PR to be just the elided lifetime warning, because the rest of this is redundant!

@wez
Copy link
Contributor Author
wez commented Apr 15, 2025

Actually, let's just do a separate branch pr, that way I can keep the one around in my fork for posterity!
#6

@wez wez closed this Apr 15, 2025
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.

2 participants
0