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 "std" feature adding requirements that don't work in std #4

Merged
merged 7 commits into from
Dec 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ rust:
- stable
- beta
- nightly
addons:
apt:
packages:
- gcc-arm-none-eabi
- libnewlib-arm-none-eabi
script: |
cargo build --verbose &&
cargo test --verbose &&
cargo doc --verbose
cargo doc --verbose &&
cargo build --verbose --no-default-features &&
cargo test --verbose --no-default-features &&
cargo doc --verbose --no-default-features &&
rustup target add thumbv7em-none-eabihf &&
cargo build --verbose --no-default-features --target thumbv7em-none-eabihf

matrix:
include:
Expand Down
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ keywords = ["crypto", "curve25519", "rust-crypto"]
categories = ["no-std", "algorithms"]
repository = "https://github.com/shekohex/curve25519-rs"

[dependencies]
rand = "0.6.1"
[dependencies.rand]
version = "0.6.1"
default-features = false

[build-dependencies]
cc = "1.0.26"

[dev-dependencies]
criterion = "0.2"

[features]
std = ["rand/std"]
default = ["std"]

[[bench]]
name = "curve25519"
harness = false
harness = false
25 changes: 21 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ use core::{
cmp::{min, Eq, PartialEq},
ops::{Add, Mul, Sub},
};
use rand::{rngs::OsRng, Error as RndError, Rng};

#[allow(unused_imports)]
use rand::{Error as RndError, ErrorKind::Unavailable, Rng};

#[cfg(feature = "std")]
use rand::rngs::OsRng;

/// Here the field is \Z/(2^255-19).
///
Expand All @@ -30,7 +35,7 @@ impl PartialEq for FieldElement {
fn eq(&self, other: &FieldElement) -> bool {
let &FieldElement(self_elems) = self;
let &FieldElement(other_elems) = other;
self_elems.to_vec() == other_elems.to_vec()
self_elems == other_elems
}
}

Expand Down Expand Up @@ -2634,6 +2639,9 @@ pub fn curve25519(secret: [u8; 32], public: [u8; 32]) -> [u8; 32] {
/// ```rust
/// # use self::curve25519::curve25519_sk;
/// # use rand::Error as RndError;
/// # #[cfg(not(feature = "std"))]
/// # fn main() { }
/// # #[cfg(feature = "std")]
/// # fn main() -> Result<(), RndError> {
/// // Let curve25519_sk generate the random 32-byte value.
/// let sk1 = curve25519_sk(None)?;
Expand All @@ -2646,17 +2654,26 @@ pub fn curve25519(secret: [u8; 32], public: [u8; 32]) -> [u8; 32] {
/// # }
/// ```
pub fn curve25519_sk(rand: Option<[u8; 32]>) -> Result<[u8; 32], RndError> {
let mut buf: [u8; 32] = [0; 32];

// Fill a 32-byte buffer with random values if necessary.
// Otherwise, use the given 32-byte value.
let mut rand: [u8; 32] = match rand {
Some(r) => r,

#[cfg(feature = "std")]
None => {
let mut rng = OsRng::new()?;
let mut buf: [u8; 32] = [0; 32];
rng.fill(&mut buf);
buf
},

#[cfg(not(feature = "std"))]
None => {
return Err(RndError::new(
Unavailable,
"Cannot generate random without Standard Library",
));
},
};

// curve25519 secret key bit manip.
Expand Down