aboutsummaryrefslogtreecommitdiff
path: root/spake2/src/lib.rs
diff options
context:
space:
mode:
authorTony Arcieri <bascule@gmail.com>2022-01-22 15:26:31 -0700
committerGitHub <noreply@github.com>2022-01-22 15:26:31 -0700
commit17292b56f52ceab154cb75239f690130df87e8a6 (patch)
tree8b118fdfe1d1c53eeb3aa32d02340ab7311f10ce /spake2/src/lib.rs
parente02188cfaed91927083cbecac9f878a6d8d71958 (diff)
downloadPAKEs-17292b56f52ceab154cb75239f690130df87e8a6.tar.xz
spake2: getrandom feature (#88)
Makes `getrandom` an optional on-by-default feature Adds CI for `thumbv7em-none-eabi` targets
Diffstat (limited to 'spake2/src/lib.rs')
-rw-r--r--spake2/src/lib.rs56
1 files changed, 46 insertions, 10 deletions
diff --git a/spake2/src/lib.rs b/spake2/src/lib.rs
index 61f7973..8a8654a 100644
--- a/spake2/src/lib.rs
+++ b/spake2/src/lib.rs
@@ -1,6 +1,10 @@
#![no_std]
-#![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
+#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
+#![doc(
+ html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
+ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
+)]
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms, unused_qualifications)]
@@ -234,9 +238,12 @@ use curve25519_dalek::{
scalar::Scalar as c2_Scalar,
};
use hkdf::Hkdf;
-use rand_core::{CryptoRng, OsRng, RngCore};
+use rand_core::{CryptoRng, RngCore};
use sha2::{Digest, Sha256};
+#[cfg(feature = "getrandom")]
+use rand_core::OsRng;
+
/* "newtype pattern": it's a Vec<u8>, but only used for a specific argument
* type, to distinguish between ones that are meant as passwords, and ones
* that are meant as identity strings */
@@ -641,21 +648,50 @@ impl<G: Group> SPAKE2<G> {
)
}
+ #[cfg(feature = "getrandom")]
+ #[cfg_attr(docsrs, doc(cfg(feature = "getrandom")))]
pub fn start_a(password: &Password, id_a: &Identity, id_b: &Identity) -> (SPAKE2<G>, Vec<u8>) {
- let mut cspring = OsRng;
- let xy_scalar: G::Scalar = G::random_scalar(&mut cspring);
- Self::start_a_internal(password, id_a, id_b, xy_scalar)
+ Self::start_a_with_rng(password, id_a, id_b, OsRng)
}
+ #[cfg(feature = "getrandom")]
+ #[cfg_attr(docsrs, doc(cfg(feature = "getrandom")))]
pub fn start_b(password: &Password, id_a: &Identity, id_b: &Identity) -> (SPAKE2<G>, Vec<u8>) {
- let mut cspring = OsRng;
- let xy_scalar: G::Scalar = G::random_scalar(&mut cspring);
- Self::start_b_internal(password, id_a, id_b, xy_scalar)
+ Self::start_b_with_rng(password, id_a, id_b, OsRng)
}
+ #[cfg(feature = "getrandom")]
+ #[cfg_attr(docsrs, doc(cfg(feature = "getrandom")))]
pub fn start_symmetric(password: &Password, id_s: &Identity) -> (SPAKE2<G>, Vec<u8>) {
- let mut cspring = OsRng;
- let xy_scalar: G::Scalar = G::random_scalar(&mut cspring);
+ Self::start_symmetric_with_rng(password, id_s, OsRng)
+ }
+
+ pub fn start_a_with_rng(
+ password: &Password,
+ id_a: &Identity,
+ id_b: &Identity,
+ mut csprng: impl CryptoRng + RngCore,
+ ) -> (SPAKE2<G>, Vec<u8>) {
+ let xy_scalar: G::Scalar = G::random_scalar(&mut csprng);
+ Self::start_a_internal(password, id_a, id_b, xy_scalar)
+ }
+
+ pub fn start_b_with_rng(
+ password: &Password,
+ id_a: &Identity,
+ id_b: &Identity,
+ mut csprng: impl CryptoRng + RngCore,
+ ) -> (SPAKE2<G>, Vec<u8>) {
+ let xy_scalar: G::Scalar = G::random_scalar(&mut csprng);
+ Self::start_b_internal(password, id_a, id_b, xy_scalar)
+ }
+
+ pub fn start_symmetric_with_rng(
+ password: &Password,
+ id_s: &Identity,
+ mut csprng: impl CryptoRng + RngCore,
+ ) -> (SPAKE2<G>, Vec<u8>) {
+ let xy_scalar: G::Scalar = G::random_scalar(&mut csprng);
Self::start_symmetric_internal(password, id_s, xy_scalar)
}