Add Zeroize to Share and GF256 (#19)
* Add Zeroize crate to project behind a default feature flag Closes https://github.com/c0dearm/sharks/issues/8 * Switch match to Clone to add zeroize(drop) * Add zeroize(drop) to Share Co-authored-by: Aitor Ruano <45633475+c0dearm@users.noreply.github.com>
This commit is contained in:
parent
1f86cadf23
commit
775333fe17
|
@ -17,14 +17,16 @@ codecov = { repository = "c0dearm/sharks" }
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
[features]
|
[features]
|
||||||
default = ["std"]
|
default = ["std", "zeroize_memory"]
|
||||||
std = ["rand/std", "rand/std_rng"]
|
std = ["rand/std", "rand/std_rng"]
|
||||||
fuzzing = ["std", "arbitrary"]
|
fuzzing = ["std", "arbitrary"]
|
||||||
|
zeroize_memory = ["zeroize"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rand = { version = "0.8", default-features = false }
|
rand = { version = "0.8", default-features = false }
|
||||||
hashbrown = "0.9"
|
hashbrown = "0.9"
|
||||||
arbitrary = { version = "0.4.7", features = ["derive"], optional = true }
|
arbitrary = { version = "0.4.7", features = ["derive"], optional = true }
|
||||||
|
zeroize = { version = "1.2.0", features = ["zeroize_derive"], optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
criterion = "0.3"
|
criterion = "0.3"
|
||||||
|
|
|
@ -7,6 +7,9 @@ use core::ops::{Add, Div, Mul, Sub};
|
||||||
#[cfg(feature = "fuzzing")]
|
#[cfg(feature = "fuzzing")]
|
||||||
use arbitrary::Arbitrary;
|
use arbitrary::Arbitrary;
|
||||||
|
|
||||||
|
#[cfg(feature = "zeroize_memory")]
|
||||||
|
use zeroize::Zeroize;
|
||||||
|
|
||||||
const LOG_TABLE: [u8; 256] = [
|
const LOG_TABLE: [u8; 256] = [
|
||||||
0x00, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b,
|
0x00, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b,
|
||||||
0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71,
|
0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71,
|
||||||
|
@ -61,8 +64,10 @@ const EXP_TABLE: [u8; 512] = [
|
||||||
0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01, 0x02,
|
0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01, 0x02,
|
||||||
];
|
];
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
#[cfg_attr(feature = "fuzzing", derive(Arbitrary))]
|
#[cfg_attr(feature = "fuzzing", derive(Arbitrary))]
|
||||||
|
#[cfg_attr(feature = "zeroize_memory", derive(Zeroize))]
|
||||||
|
#[cfg_attr(feature = "zeroize_memory", zeroize(drop))]
|
||||||
pub struct GF256(pub u8);
|
pub struct GF256(pub u8);
|
||||||
|
|
||||||
#[allow(clippy::suspicious_arithmetic_impl)]
|
#[allow(clippy::suspicious_arithmetic_impl)]
|
||||||
|
|
|
@ -201,7 +201,7 @@ mod tests {
|
||||||
let sharks = Sharks(255);
|
let sharks = Sharks(255);
|
||||||
let mut shares: Vec<Share> = sharks.make_shares(&[1]).take(255).collect();
|
let mut shares: Vec<Share> = sharks.make_shares(&[1]).take(255).collect();
|
||||||
shares[1] = Share {
|
shares[1] = Share {
|
||||||
x: shares[0].x,
|
x: shares[0].x.clone(),
|
||||||
y: shares[0].y.clone(),
|
y: shares[0].y.clone(),
|
||||||
};
|
};
|
||||||
let secret = sharks.recover(&shares);
|
let secret = sharks.recover(&shares);
|
||||||
|
|
13
src/math.rs
13
src/math.rs
|
@ -20,9 +20,9 @@ pub fn interpolate(shares: &[Share]) -> Vec<u8> {
|
||||||
shares
|
shares
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|s_j| s_j.x != s_i.x)
|
.filter(|s_j| s_j.x != s_i.x)
|
||||||
.map(|s_j| s_j.x / (s_j.x - s_i.x))
|
.map(|s_j| s_j.x.clone() / (s_j.x.clone() - s_i.x.clone()))
|
||||||
.product::<GF256>()
|
.product::<GF256>()
|
||||||
* s_i.y[s]
|
* s_i.y[s].clone()
|
||||||
})
|
})
|
||||||
.sum::<GF256>()
|
.sum::<GF256>()
|
||||||
.0
|
.0
|
||||||
|
@ -51,10 +51,13 @@ pub fn random_polynomial<R: rand::Rng>(s: GF256, k: u8, rng: &mut R) -> Vec<GF25
|
||||||
// The iterator will start at `x = 1` and end at `x = 255`.
|
// The iterator will start at `x = 1` and end at `x = 255`.
|
||||||
pub fn get_evaluator(polys: Vec<Vec<GF256>>) -> impl Iterator<Item = Share> {
|
pub fn get_evaluator(polys: Vec<Vec<GF256>>) -> impl Iterator<Item = Share> {
|
||||||
(1..=u8::max_value()).map(GF256).map(move |x| Share {
|
(1..=u8::max_value()).map(GF256).map(move |x| Share {
|
||||||
x,
|
x: x.clone(),
|
||||||
y: polys
|
y: polys
|
||||||
.iter()
|
.iter()
|
||||||
.map(|p| p.iter().fold(GF256(0), |acc, c| acc * x + *c))
|
.map(|p| {
|
||||||
|
p.iter()
|
||||||
|
.fold(GF256(0), |acc, c| acc * x.clone() + c.clone())
|
||||||
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -76,7 +79,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn evaluator_works() {
|
fn evaluator_works() {
|
||||||
let iter = get_evaluator(vec![vec![GF256(3), GF256(2), GF256(5)]]);
|
let iter = get_evaluator(vec![vec![GF256(3), GF256(2), GF256(5)]]);
|
||||||
let values: Vec<_> = iter.take(2).map(|s| (s.x, s.y)).collect();
|
let values: Vec<_> = iter.take(2).map(|s| (s.x.clone(), s.y.clone())).collect();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
values,
|
values,
|
||||||
vec![(GF256(1), vec![GF256(4)]), (GF256(2), vec![GF256(13)])]
|
vec![(GF256(1), vec![GF256(4)]), (GF256(2), vec![GF256(13)])]
|
||||||
|
|
|
@ -5,6 +5,9 @@ use super::field::GF256;
|
||||||
#[cfg(feature = "fuzzing")]
|
#[cfg(feature = "fuzzing")]
|
||||||
use arbitrary::Arbitrary;
|
use arbitrary::Arbitrary;
|
||||||
|
|
||||||
|
#[cfg(feature = "zeroize_memory")]
|
||||||
|
use zeroize::Zeroize;
|
||||||
|
|
||||||
/// A share used to reconstruct the secret. Can be serialized to and from a byte array.
|
/// A share used to reconstruct the secret. Can be serialized to and from a byte array.
|
||||||
///
|
///
|
||||||
/// Usage example:
|
/// Usage example:
|
||||||
|
@ -31,6 +34,8 @@ use arbitrary::Arbitrary;
|
||||||
/// let secret = sharks.recover(&shares).unwrap();
|
/// let secret = sharks.recover(&shares).unwrap();
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
#[cfg_attr(feature = "fuzzing", derive(Arbitrary, Debug))]
|
#[cfg_attr(feature = "fuzzing", derive(Arbitrary, Debug))]
|
||||||
|
#[cfg_attr(feature = "zeroize_memory", derive(Zeroize))]
|
||||||
|
#[cfg_attr(feature = "zeroize_memory", zeroize(drop))]
|
||||||
pub struct Share {
|
pub struct Share {
|
||||||
pub x: GF256,
|
pub x: GF256,
|
||||||
pub y: Vec<GF256>,
|
pub y: Vec<GF256>,
|
||||||
|
|
Loading…
Reference in New Issue