mirror of
https://github.com/saymrwulf/risc0-curve25519-dalek-source.git
synced 2026-09-07 20:30:39 +00:00
implement Zeroize for Scalar and MontgomeryPoint
This commit is contained in:
parent
6239a92055
commit
ba389040de
5 changed files with 25 additions and 3 deletions
|
|
@ -48,6 +48,7 @@ clear_on_drop = "=0.2.3"
|
||||||
subtle = { version = "2.0.0-pre.0", default-features = false }
|
subtle = { version = "2.0.0-pre.0", default-features = false }
|
||||||
serde = { version = "1.0", optional = true }
|
serde = { version = "1.0", optional = true }
|
||||||
packed_simd = { version = "0.3.0", features = ["into_bits"], optional = true }
|
packed_simd = { version = "0.3.0", features = ["into_bits"], optional = true }
|
||||||
|
zeroize = { version = "0.5.2", default-features = false }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
rand = { version = "0.6.0", default-features = false }
|
rand = { version = "0.6.0", default-features = false }
|
||||||
|
|
@ -57,9 +58,10 @@ clear_on_drop = "=0.2.3"
|
||||||
subtle = { version = "2.0.0-pre.0", default-features = false }
|
subtle = { version = "2.0.0-pre.0", default-features = false }
|
||||||
serde = { version = "1.0", optional = true }
|
serde = { version = "1.0", optional = true }
|
||||||
packed_simd = { version = "0.3.0", features = ["into_bits"], optional = true }
|
packed_simd = { version = "0.3.0", features = ["into_bits"], optional = true }
|
||||||
|
zeroize = { version = "0.5.2", default-features = false }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
nightly = ["subtle/nightly", "clear_on_drop/nightly"]
|
nightly = ["subtle/nightly", "clear_on_drop/nightly", "zeroize/nightly"]
|
||||||
default = ["std", "u64_backend"]
|
default = ["std", "u64_backend"]
|
||||||
std = ["alloc", "subtle/std", "rand/std"]
|
std = ["alloc", "subtle/std", "rand/std"]
|
||||||
alloc = []
|
alloc = []
|
||||||
|
|
|
||||||
2
build.rs
2
build.rs
|
|
@ -16,6 +16,8 @@ extern crate subtle;
|
||||||
#[cfg(all(feature = "nightly", feature = "avx2_backend"))]
|
#[cfg(all(feature = "nightly", feature = "avx2_backend"))]
|
||||||
extern crate packed_simd;
|
extern crate packed_simd;
|
||||||
|
|
||||||
|
extern crate zeroize;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,8 @@ extern crate serde;
|
||||||
#[cfg(all(test, feature = "serde"))]
|
#[cfg(all(test, feature = "serde"))]
|
||||||
extern crate bincode;
|
extern crate bincode;
|
||||||
|
|
||||||
|
extern crate zeroize;
|
||||||
|
|
||||||
// Internal macros. Must come first!
|
// Internal macros. Must come first!
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub(crate) mod macros;
|
pub(crate) mod macros;
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
//! Montgomery arithmetic works not on the curve itself, but on the
|
//! Montgomery arithmetic works not on the curve itself, but on the
|
||||||
//! \\(u\\)-line, which discards sign information and unifies the curve
|
//! \\(u\\)-line, which discards sign information and unifies the curve
|
||||||
//! and its quadratic twist. See [_Montgomery curves and their
|
//! and its quadratic twist. See [_Montgomery curves and their
|
||||||
//! arithmetic_][costello-smith] by Costello and Smith for more details.
|
//! arithmetic_][costello-smith] by Costello and Smith for more details.
|
||||||
//!
|
//!
|
||||||
//! The `MontgomeryPoint` struct contains the affine \\(u\\)-coordinate
|
//! The `MontgomeryPoint` struct contains the affine \\(u\\)-coordinate
|
||||||
//! \\(u\_0(P)\\) of a point \\(P\\) on either the curve or the twist.
|
//! \\(u\_0(P)\\) of a point \\(P\\) on either the curve or the twist.
|
||||||
|
|
@ -61,6 +61,8 @@ use subtle::Choice;
|
||||||
use subtle::ConditionallySelectable;
|
use subtle::ConditionallySelectable;
|
||||||
use subtle::ConstantTimeEq;
|
use subtle::ConstantTimeEq;
|
||||||
|
|
||||||
|
use zeroize::Zeroize;
|
||||||
|
|
||||||
/// Holds the \\(u\\)-coordinate of a point on the Montgomery form of
|
/// Holds the \\(u\\)-coordinate of a point on the Montgomery form of
|
||||||
/// Curve25519 or its twist.
|
/// Curve25519 or its twist.
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
|
@ -90,6 +92,12 @@ impl PartialEq for MontgomeryPoint {
|
||||||
|
|
||||||
impl Eq for MontgomeryPoint {}
|
impl Eq for MontgomeryPoint {}
|
||||||
|
|
||||||
|
impl Zeroize for MontgomeryPoint {
|
||||||
|
fn zeroize(&mut self) {
|
||||||
|
self.0.zeroize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MontgomeryPoint {
|
impl MontgomeryPoint {
|
||||||
/// View this `MontgomeryPoint` as an array of bytes.
|
/// View this `MontgomeryPoint` as an array of bytes.
|
||||||
pub fn as_bytes<'a>(&'a self) -> &'a [u8; 32] {
|
pub fn as_bytes<'a>(&'a self) -> &'a [u8; 32] {
|
||||||
|
|
@ -335,7 +343,7 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn montgomery_to_edwards_rejects_twist() {
|
fn montgomery_to_edwards_rejects_twist() {
|
||||||
let one = FieldElement::one();
|
let one = FieldElement::one();
|
||||||
|
|
||||||
// u = 2 corresponds to a point on the twist.
|
// u = 2 corresponds to a point on the twist.
|
||||||
let two = MontgomeryPoint((&one+&one).to_bytes());
|
let two = MontgomeryPoint((&one+&one).to_bytes());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -160,6 +160,8 @@ use subtle::Choice;
|
||||||
use subtle::ConditionallySelectable;
|
use subtle::ConditionallySelectable;
|
||||||
use subtle::ConstantTimeEq;
|
use subtle::ConstantTimeEq;
|
||||||
|
|
||||||
|
use zeroize::Zeroize;
|
||||||
|
|
||||||
use backend;
|
use backend;
|
||||||
use constants;
|
use constants;
|
||||||
|
|
||||||
|
|
@ -502,6 +504,12 @@ impl From<u128> for Scalar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Zeroize for Scalar {
|
||||||
|
fn zeroize(&mut self) {
|
||||||
|
self.bytes.zeroize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Scalar {
|
impl Scalar {
|
||||||
/// Return a `Scalar` chosen uniformly at random using a user-provided RNG.
|
/// Return a `Scalar` chosen uniformly at random using a user-provided RNG.
|
||||||
///
|
///
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue