]> git.lizzy.rs Git - PAKEs.git/commitdiff
spake2: getrandom feature (#88)
authorTony Arcieri <bascule@gmail.com>
Sat, 22 Jan 2022 22:26:31 +0000 (15:26 -0700)
committerGitHub <noreply@github.com>
Sat, 22 Jan 2022 22:26:31 +0000 (15:26 -0700)
Makes `getrandom` an optional on-by-default feature

Adds CI for `thumbv7em-none-eabi` targets

.github/workflows/spake2.yml
spake2/Cargo.toml
spake2/src/lib.rs

index 6aaf07666e2212265c1c227f57affa650026a094..34968fd28726222ee8fd95e1a9ea6971078822d9 100644 (file)
@@ -25,6 +25,7 @@ jobs:
           - 1.56.0 # MSRV
           - stable
         target:
+          - thumbv7em-none-eabi
           - wasm32-unknown-unknown
     steps:
       - uses: actions/checkout@v1
@@ -34,7 +35,7 @@ jobs:
           toolchain: ${{ matrix.rust }}
           target: ${{ matrix.target }}
           override: true
-      - run: cargo build --target ${{ matrix.target }} --release
+      - run: cargo build --target ${{ matrix.target }} --release --no-default-features
 
   test:
     runs-on: ubuntu-latest
@@ -51,3 +52,4 @@ jobs:
           override: true
           profile: minimal
       - run: cargo test --release
+      - run: cargo test --release --all-features
index 0558737a8cc024295ab0332d5ecb391c584c897b..5547cfa43ef8ed5b9b570b6c4357c32b6e2acb2e 100644 (file)
@@ -16,9 +16,9 @@ rust-version = "1.56"
 
 [dependencies]
 curve25519-dalek = { version = "3", default-features = false, features = ["u64_backend"] }
-rand_core = { version = "0.5", default-features = false, features = ["getrandom"] }
-sha2 = "0.10"
-hkdf = "0.12"
+rand_core = { version = "0.5", default-features = false }
+sha2 = { version = "0.10", default-features = false }
+hkdf = { version = "0.12", default-features = false }
 
 [dev-dependencies]
 bencher = "0.1"
@@ -26,9 +26,14 @@ hex = "0.4"
 num-bigint = "0.4"
 
 [features]
-default = []
+default = ["getrandom"]
+getrandom = ["rand_core/getrandom"]
 std = []
 
+[package.metadata.docs.rs]
+all-features = true
+rustdoc-args = ["--cfg", "docsrs"]
+
 [[bench]]
 name = "spake2"
 harness = false
index 61f7973ff21aefe51a8e37d6ff9833ff4489fa70..8a8654a6fa734de971e11e3bb2ed4fd71002b6f6 100644 (file)
@@ -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)
     }