Prepare for adding mock cryptography for testing.

* Add a feature flag (that doesn't do anything yet).
* Give `clear_fr` a stronger type to prevent mistakes and ensure the
  right amount of memory gets cleared.
* Re-export the engine and related types from `lib.rs`.
This commit is contained in:
Andreas Fackler 2018-09-26 14:07:12 +02:00 committed by Andreas Fackler
parent d69590bedc
commit 0efdad4d69
6 changed files with 23 additions and 20 deletions

View File

@ -19,6 +19,7 @@ env:
script: script:
- cargo clippy --tests --examples --benches -- --deny clippy - cargo clippy --tests --examples --benches -- --deny clippy
- cargo fmt -- --check - cargo fmt -- --check
- cargo test --release
- cargo test --all-features --release - cargo test --all-features --release
- cargo doc - cargo doc
- cargo deadlinks --dir target/doc/threshold_crypto/ - cargo deadlinks --dir target/doc/threshold_crypto/

View File

@ -39,3 +39,6 @@ serde_derive = "1.0.55"
[[bench]] [[bench]]
name = "bench" name = "bench"
harness = false harness = false
[features]
use-insecure-test-only-mock-crypto = []

View File

@ -5,8 +5,8 @@ extern crate rand;
extern crate threshold_crypto; extern crate threshold_crypto;
use criterion::Criterion; use criterion::Criterion;
use pairing::bls12_381::Fr;
use threshold_crypto::poly::Poly; use threshold_crypto::poly::Poly;
use threshold_crypto::Fr;
const TEST_DEGREES: [usize; 4] = [5, 10, 20, 40]; const TEST_DEGREES: [usize; 4] = [5, 10, 20, 40];
const TEST_THRESHOLDS: [usize; 4] = [5, 10, 20, 40]; const TEST_THRESHOLDS: [usize; 4] = [5, 10, 20, 40];

View File

@ -35,15 +35,16 @@ use std::ptr::copy_nonoverlapping;
use byteorder::{BigEndian, ByteOrder}; use byteorder::{BigEndian, ByteOrder};
use init_with::InitWith; use init_with::InitWith;
use memsec::{memzero, mlock, munlock};
use pairing::bls12_381::Bls12 as PEngine; // #[cfg(not(feature = "use-insecure-test-only-mock-crypto"))]
type Fq = pairing::bls12_381::Fq; pub use pairing::bls12_381::{Bls12 as PEngine, Fr, G1Affine, G2Affine, G1, G2};
type Fr = pairing::bls12_381::Fr;
type G1 = pairing::bls12_381::G1; // TODO: Add mock cryptography for tests.
type G1Affine = pairing::bls12_381::G1Affine; // #[cfg(feature = "use-insecure-test-only-mock-crypto")]
type G2 = pairing::bls12_381::G2; // pub use pairing::mock::{
type G2Affine = pairing::bls12_381::G2Affine; // Mersenne8 as Fr, Mocktography as PEngine, Ms8Affine as G1Affine, Ms8Affine as G2Affine,
// Ms8Projective as G1, Ms8Projective as G2,
// };
use pairing::{CurveAffine, CurveProjective, Engine, Field}; use pairing::{CurveAffine, CurveProjective, Engine, Field};
use rand::{ChaChaRng, OsRng, Rand, Rng, SeedableRng}; use rand::{ChaChaRng, OsRng, Rand, Rng, SeedableRng};
@ -341,7 +342,7 @@ impl SecretKey {
unsafe { unsafe {
copy_nonoverlapping(fr_ptr, &mut *boxed_fr as *mut Fr, 1); copy_nonoverlapping(fr_ptr, &mut *boxed_fr as *mut Fr, 1);
} }
clear_fr(fr_ptr as *mut u8); clear_fr(fr_ptr);
let sk = SecretKey(boxed_fr); let sk = SecretKey(boxed_fr);
sk.mlock_secret()?; sk.mlock_secret()?;
Ok(sk) Ok(sk)

View File

@ -23,8 +23,6 @@ use std::mem::size_of_val;
use std::{cmp, iter, ops}; use std::{cmp, iter, ops};
use super::{Fr, G1Affine, G1}; use super::{Fr, G1Affine, G1};
use errno::errno;
use memsec::{memzero, mlock, munlock};
use pairing::{CurveAffine, CurveProjective, Field}; use pairing::{CurveAffine, CurveProjective, Field};
use rand::Rng; use rand::Rng;
@ -78,7 +76,7 @@ impl<B: Borrow<Poly>> ops::AddAssign<B> for Poly {
} }
} }
for (self_c, rhs_c) in self.coeff.iter_mut().zip(&rhs.borrow().coeff) { for (self_c, rhs_c) in self.coeff.iter_mut().zip(&rhs.borrow().coeff) {
self_c.add_assign(rhs_c); Field::add_assign(self_c, rhs_c);
} }
self.remove_zeros(); self.remove_zeros();
} }
@ -146,7 +144,7 @@ impl<B: Borrow<Poly>> ops::SubAssign<B> for Poly {
} }
} }
for (self_c, rhs_c) in self.coeff.iter_mut().zip(&rhs.borrow().coeff) { for (self_c, rhs_c) in self.coeff.iter_mut().zip(&rhs.borrow().coeff) {
self_c.sub_assign(rhs_c); Field::sub_assign(self_c, rhs_c);
} }
self.remove_zeros(); self.remove_zeros();
} }
@ -237,7 +235,7 @@ impl ops::MulAssign<Fr> for Poly {
self.coeff.clear(); self.coeff.clear();
} else { } else {
for c in &mut self.coeff { for c in &mut self.coeff {
c.mul_assign(&rhs); Field::mul_assign(c, &rhs);
} }
} }
} }
@ -415,7 +413,7 @@ impl Poly {
// We create a raw pointer to the field element within this method's stack frame so we can // We create a raw pointer to the field element within this method's stack frame so we can
// overwrite that portion of memory with zeros once we have copied the element onto the // overwrite that portion of memory with zeros once we have copied the element onto the
// heap as part of the vector of polynomial coefficients. // heap as part of the vector of polynomial coefficients.
let fr_ptr = &c as *const Fr as *mut u8; let fr_ptr = &c as *const Fr;
let poly = Poly::try_from(vec![c]) let poly = Poly::try_from(vec![c])
.unwrap_or_else(|e| panic!("Failed to create constant `Poly`: {}", e)); .unwrap_or_else(|e| panic!("Failed to create constant `Poly`: {}", e));
clear_fr(fr_ptr); clear_fr(fr_ptr);
@ -433,7 +431,7 @@ impl Poly {
// We create a raw pointer to the field element within this method's stack frame so we can // We create a raw pointer to the field element within this method's stack frame so we can
// overwrite that portion of memory with zeros once we have copied the element onto the // overwrite that portion of memory with zeros once we have copied the element onto the
// heap as part of polynomials `coeff` vector. // heap as part of polynomials `coeff` vector.
let fr_ptr = &c as *const Fr as *mut u8; let fr_ptr = &c as *const Fr;
let res = Poly::try_from(vec![c]); let res = Poly::try_from(vec![c]);
clear_fr(fr_ptr); clear_fr(fr_ptr);
res res

View File

@ -7,7 +7,7 @@ use std::ops::{Deref, DerefMut};
use errno::errno; use errno::errno;
use memsec::{memzero, mlock, munlock}; use memsec::{memzero, mlock, munlock};
use pairing::bls12_381::Fr; use Fr;
use error::{Error, Result}; use error::{Error, Result};
@ -32,8 +32,8 @@ lazy_static! {
} }
/// Overwrites a single field element with zeros. /// Overwrites a single field element with zeros.
pub(crate) fn clear_fr(fr_ptr: *mut u8) { pub(crate) fn clear_fr(fr_ptr: *const Fr) {
unsafe { memzero(fr_ptr, *FR_SIZE) }; unsafe { memzero(fr_ptr as *mut u8, *FR_SIZE) };
} }
pub(crate) struct MemRange { pub(crate) struct MemRange {