mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-05 20:30:57 +00:00
Switch to using new digest v0.5 API.
This commit is contained in:
parent
641e409ceb
commit
a195249468
2 changed files with 24 additions and 23 deletions
|
|
@ -27,7 +27,7 @@ optional = true
|
||||||
version = "^0.3"
|
version = "^0.3"
|
||||||
|
|
||||||
[dependencies.digest]
|
[dependencies.digest]
|
||||||
version = "0.4"
|
version = "^0.5"
|
||||||
|
|
||||||
[dependencies.generic-array]
|
[dependencies.generic-array]
|
||||||
# same version that digest depends on
|
# same version that digest depends on
|
||||||
|
|
@ -35,7 +35,7 @@ version = "^0.6"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rustc-serialize = "0.3"
|
rustc-serialize = "0.3"
|
||||||
sha2 = "^0.4"
|
sha2 = "^0.5"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["std"]
|
default = ["std"]
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,8 @@ use core::fmt::Debug;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use digest::Digest;
|
use digest::Input;
|
||||||
|
use digest::FixedOutput;
|
||||||
use generic_array::typenum::U64;
|
use generic_array::typenum::U64;
|
||||||
|
|
||||||
use curve25519_dalek::curve;
|
use curve25519_dalek::curve;
|
||||||
|
|
@ -137,7 +138,7 @@ impl SecretKey {
|
||||||
|
|
||||||
/// Sign a message with this keypair's secret key.
|
/// Sign a message with this keypair's secret key.
|
||||||
pub fn sign<D>(&self, message: &[u8]) -> Signature
|
pub fn sign<D>(&self, message: &[u8]) -> Signature
|
||||||
where D: Digest<OutputSize = U64> + Default {
|
where D: FixedOutput<OutputSize = U64> + Default + Input {
|
||||||
|
|
||||||
let mut h: D = D::default();
|
let mut h: D = D::default();
|
||||||
let mut hash: [u8; 64] = [0u8; 64];
|
let mut hash: [u8; 64] = [0u8; 64];
|
||||||
|
|
@ -152,8 +153,8 @@ impl SecretKey {
|
||||||
let secret_key: &[u8; 32] = array_ref!(&self.0, 0, 32);
|
let secret_key: &[u8; 32] = array_ref!(&self.0, 0, 32);
|
||||||
let public_key: &[u8; 32] = array_ref!(&self.0, 32, 32);
|
let public_key: &[u8; 32] = array_ref!(&self.0, 32, 32);
|
||||||
|
|
||||||
h.input(secret_key);
|
h.digest(secret_key);
|
||||||
hash.copy_from_slice(h.result().as_slice());
|
hash.copy_from_slice(h.fixed_result().as_slice());
|
||||||
|
|
||||||
expanded_key_secret = Scalar(*array_ref!(&hash, 0, 32));
|
expanded_key_secret = Scalar(*array_ref!(&hash, 0, 32));
|
||||||
expanded_key_secret[0] &= 248;
|
expanded_key_secret[0] &= 248;
|
||||||
|
|
@ -161,19 +162,19 @@ impl SecretKey {
|
||||||
expanded_key_secret[31] |= 64;
|
expanded_key_secret[31] |= 64;
|
||||||
|
|
||||||
h = D::default();
|
h = D::default();
|
||||||
h.input(&hash[32..]);
|
h.digest(&hash[32..]);
|
||||||
h.input(&message);
|
h.digest(&message);
|
||||||
hash.copy_from_slice(h.result().as_slice());
|
hash.copy_from_slice(h.fixed_result().as_slice());
|
||||||
|
|
||||||
mesg_digest = Scalar::reduce(&hash);
|
mesg_digest = Scalar::reduce(&hash);
|
||||||
|
|
||||||
r = ExtendedPoint::basepoint_mult(&mesg_digest);
|
r = ExtendedPoint::basepoint_mult(&mesg_digest);
|
||||||
|
|
||||||
h = D::default();
|
h = D::default();
|
||||||
h.input(&r.compress_edwards().to_bytes()[..]);
|
h.digest(&r.compress_edwards().to_bytes()[..]);
|
||||||
h.input(public_key);
|
h.digest(public_key);
|
||||||
h.input(&message);
|
h.digest(&message);
|
||||||
hash.copy_from_slice(h.result().as_slice());
|
hash.copy_from_slice(h.fixed_result().as_slice());
|
||||||
|
|
||||||
hram_digest = Scalar::reduce(&hash);
|
hram_digest = Scalar::reduce(&hash);
|
||||||
|
|
||||||
|
|
@ -245,7 +246,7 @@ impl PublicKey {
|
||||||
/// Returns true if the signature was successfully verified, and
|
/// Returns true if the signature was successfully verified, and
|
||||||
/// false otherwise.
|
/// false otherwise.
|
||||||
pub fn verify<D>(&self, message: &[u8], signature: &Signature) -> bool
|
pub fn verify<D>(&self, message: &[u8], signature: &Signature) -> bool
|
||||||
where D: Digest<OutputSize = U64> + Default {
|
where D: FixedOutput<OutputSize = U64> + Default + Input {
|
||||||
|
|
||||||
let mut h: D = D::default();
|
let mut h: D = D::default();
|
||||||
let mut a: ExtendedPoint;
|
let mut a: ExtendedPoint;
|
||||||
|
|
@ -269,11 +270,11 @@ impl PublicKey {
|
||||||
let top_half: &[u8; 32] = array_ref!(&signature.0, 32, 32);
|
let top_half: &[u8; 32] = array_ref!(&signature.0, 32, 32);
|
||||||
let bottom_half: &[u8; 32] = array_ref!(&signature.0, 0, 32);
|
let bottom_half: &[u8; 32] = array_ref!(&signature.0, 0, 32);
|
||||||
|
|
||||||
h.input(&bottom_half[..]);
|
h.digest(&bottom_half[..]);
|
||||||
h.input(&self.to_bytes());
|
h.digest(&self.to_bytes());
|
||||||
h.input(&message);
|
h.digest(&message);
|
||||||
|
|
||||||
let digest_bytes = h.result();
|
let digest_bytes = h.fixed_result();
|
||||||
digest = *array_ref!(digest_bytes, 0, 64);
|
digest = *array_ref!(digest_bytes, 0, 64);
|
||||||
digest_reduced = Scalar::reduce(&digest);
|
digest_reduced = Scalar::reduce(&digest);
|
||||||
r = curve::double_scalar_mult_vartime(&digest_reduced, &a, &Scalar(*top_half));
|
r = curve::double_scalar_mult_vartime(&digest_reduced, &a, &Scalar(*top_half));
|
||||||
|
|
@ -334,7 +335,7 @@ impl Keypair {
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
#[allow(unused_assignments)]
|
#[allow(unused_assignments)]
|
||||||
pub fn generate<D>(cspring: &mut Rng) -> Keypair
|
pub fn generate<D>(cspring: &mut Rng) -> Keypair
|
||||||
where D: Digest<OutputSize = U64> + Default {
|
where D: FixedOutput<OutputSize = U64> + Default + Input {
|
||||||
|
|
||||||
let mut h: D = D::default();
|
let mut h: D = D::default();
|
||||||
let mut hash: [u8; 64] = [0u8; 64];
|
let mut hash: [u8; 64] = [0u8; 64];
|
||||||
|
|
@ -345,8 +346,8 @@ impl Keypair {
|
||||||
|
|
||||||
cspring.fill_bytes(&mut t);
|
cspring.fill_bytes(&mut t);
|
||||||
|
|
||||||
h.input(&t);
|
h.digest(&t);
|
||||||
hash.copy_from_slice(h.result().as_slice());
|
hash.copy_from_slice(h.fixed_result().as_slice());
|
||||||
|
|
||||||
digest = array_mut_ref!(&mut hash, 0, 32);
|
digest = array_mut_ref!(&mut hash, 0, 32);
|
||||||
digest[0] &= 248;
|
digest[0] &= 248;
|
||||||
|
|
@ -369,13 +370,13 @@ impl Keypair {
|
||||||
|
|
||||||
/// Sign a message with this keypair's secret key.
|
/// Sign a message with this keypair's secret key.
|
||||||
pub fn sign<D>(&self, message: &[u8]) -> Signature
|
pub fn sign<D>(&self, message: &[u8]) -> Signature
|
||||||
where D: Digest<OutputSize = U64> + Default {
|
where D: FixedOutput<OutputSize = U64> + Default + Input {
|
||||||
self.secret.sign::<D>(message)
|
self.secret.sign::<D>(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Verify a signature on a message with this keypair's public key.
|
/// Verify a signature on a message with this keypair's public key.
|
||||||
pub fn verify<D>(&self, message: &[u8], signature: &Signature) -> bool
|
pub fn verify<D>(&self, message: &[u8], signature: &Signature) -> bool
|
||||||
where D: Digest<OutputSize = U64> + Default {
|
where D: FixedOutput<OutputSize = U64> + Default + Input {
|
||||||
self.public.verify::<D>(message, signature)
|
self.public.verify::<D>(message, signature)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue