Skip to content
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

Add #![no_std] (fixes #16) #17

Merged
merged 1 commit into from
Jan 27, 2017

Conversation

tarcieri
Copy link
Contributor

This crate does not depend on std. I would like to use it in a #![no_std] Rust program, but to do that this library can't link against std.

arrayref = "0.3.2"

# Support for #![no_std] is only on master right now
arrayref = { git = "https://github.com/droundy/arrayref.git" }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might want to wait for another release of arrayref before merging this /cc @droundy

Copy link
Member

@isislovecruft isislovecruft left a comment

Choose a reason for hiding this comment

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

Approved, but I'm leaning towards waiting for a new version of arrayref.

Also, is there some reason why using core instead of std would be slightly slower? The bench tests seem to be telling me it's a bit slower.

@isislovecruft
Copy link
Member

@tarcieri I was vaguely considering using https://github.com/stouset/secrets, which wouldn't be compatible. I'm not sure whether to prioritise protecting/guarding memory allocated to hold secrets or to prioritise platform independence. It seems like the optimal solution would be for higher-level crypto protocols like https://github.com/isislovecruft/ed25519-dalek to use https://github.com/stouset/secrets and for curve25519-dalek to prioritise usability anywhere. (Thoughts, @hdevalence?)

@tarcieri
Copy link
Contributor Author

tarcieri commented Jan 14, 2017

@isislovecruft you can gate features that use std via cargo features, which can even be enabled by default and selectively disabled for the #![no_std] use case. For example you could make a cargo feature called std that all of the ::std-dependent features are gated on.

Re: memory protection, you might find this thread informative: rust-lang/rfcs#766 (comment)

@burdges
Copy link
Contributor

burdges commented Jan 14, 2017

It might suffice to avoid returning secret data by value, which allow LLVM to move it around the stack willy nilly. In fact, I was just about to raise an issue mentioning that four fns random, pack_limbs, multiply_add, and reduce return a Scalar by value.

There are actually tricks where by a return by value avoids the stack, at least in release builds, like say immediately calling box on it. Anything like that sounds hard to maintain though, so providing a buffer is safer and punts the stack safety issue onto the caller.

I suspect any stack buffers allocated internally by your own code should be less risky, but one can imagine many games to address that too.

@burdges
Copy link
Contributor

burdges commented Jan 14, 2017

Also we should expect changes in the secure memory crate space as allocators slowly stabilize.

@tarcieri
Copy link
Contributor Author

It appears rand still links against std, so to make this crate truly #![no_std] we'll need to feature-gate the functionality that uses rand on std. I can update this PR with that.

@tarcieri
Copy link
Contributor Author

Ok, I've updated my PR to add an "std" cargo feature which is enabled-per-default which the rand crate is now gated on.

I have confirmed my #![no_std] project now builds and links against curve25519-dalek when default-features = false is used in Cargo.toml.

So long as any additional std-dependent crates you may use are gated on #[cfg(feature = "std")], this change should be invisible to std users while allowing the crate to build/link in #![no_std] environments.

version = "0.3"

[dependencies.arrayref]
version = "0.3.2"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went ahead and put this back for now, however the released crate still depends on std. I can build my project by pulling in arrayref via git, but FYI it'd be nice to bump this whenever there's another release.

@tarcieri
Copy link
Contributor Author

I think this is ready to merge

@tarcieri
Copy link
Contributor Author

Ok, I spoke too soon: this needs the new arrayref crate to be released before it can be merged

@droundy
Copy link

droundy commented Jan 18, 2017

I've just published arrayref 0.3.3, which should enable you to move forward.

@tarcieri
Copy link
Contributor Author

@droundy awesome thanks! I think @isislovecruft was noting a 5% performance reduction from this PR... any idea if arrayref could be responsible? (nothing else seems like a likely culprit)

Use ::core in lieu of ::std, allowing this crate to be usable in #![no_std]
environments.

Gates features that presently depend on ::std (presently just rand) behind a
"std" cargo feature, which is enabled by default.
@tarcieri
Copy link
Contributor Author

I think this is actually ready to merge now

@droundy
Copy link

droundy commented Jan 19, 2017 via email

@isislovecruft
Copy link
Member

@droundy Thanks for the new arrayref release! We've been using 0.3.2 this whole time.

@isislovecruft
Copy link
Member

isislovecruft commented Jan 27, 2017

@tarcieri I've merged and released curve25519-dalek-0.3.0. Thanks for all your help! Let me know if there's anything else we can do to make this usable for your projects.

@isislovecruft isislovecruft merged commit d08bc73 into dalek-cryptography:master Jan 27, 2017
@tarcieri tarcieri deleted the no-std branch January 28, 2017 02:13
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.

5 participants