Merge branch 'feature/curve25519-dalek-0.10' into develop

This commit is contained in:
Isis Lovecruft 2017-08-01 18:17:26 +00:00
commit 6146a55845
Failed to extract signature
3 changed files with 30 additions and 21 deletions

View file

@ -19,7 +19,11 @@ travis-ci = { repository = "isislovecruft/ed25519-dalek", branch = "master"}
arrayref = "0.3.3" arrayref = "0.3.3"
[dependencies.curve25519-dalek] [dependencies.curve25519-dalek]
version = "^0.7" version = "^0.10"
default-features = false
[dependencies.subtle]
version = "^0.2"
default-features = false default-features = false
[dependencies.rand] [dependencies.rand]
@ -27,15 +31,15 @@ optional = true
version = "^0.3" version = "^0.3"
[dependencies.digest] [dependencies.digest]
version = "^0.5" version = "^0.6"
[dependencies.generic-array] [dependencies.generic-array]
# same version that digest depends on # same version that digest depends on
version = "^0.6" version = "^0.8"
[dev-dependencies] [dev-dependencies]
rustc-serialize = "0.3" rustc-serialize = "0.3"
sha2 = "^0.5" sha2 = "^0.6"
[features] [features]
default = ["std"] default = ["std"]

View file

@ -16,15 +16,19 @@ use core::fmt::Debug;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use rand::Rng; use rand::Rng;
use digest::BlockInput;
use digest::Digest;
use digest::Input; use digest::Input;
use digest::FixedOutput; use digest::FixedOutput;
use generic_array::typenum::U64; use generic_array::typenum::U64;
use curve25519_dalek::constants; use curve25519_dalek::constants;
use curve25519_dalek::curve::CompressedEdwardsY; use curve25519_dalek::curve::CompressedEdwardsY;
use curve25519_dalek::curve::ExtendedPoint; use curve25519_dalek::curve::ExtendedPoint;
use curve25519_dalek::scalar::Scalar; use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::subtle::arrays_equal_ct;
use subtle::slices_equal;
/// The length of an ed25519 EdDSA `Signature`, in bytes. /// The length of an ed25519 EdDSA `Signature`, in bytes.
pub const SIGNATURE_LENGTH: usize = 64; pub const SIGNATURE_LENGTH: usize = 64;
@ -279,14 +283,14 @@ impl PublicKey {
#[cfg(feature = "std")] #[cfg(feature = "std")]
#[allow(unused_assignments)] #[allow(unused_assignments)]
pub fn from_secret<D>(secret_key: &SecretKey) -> PublicKey pub fn from_secret<D>(secret_key: &SecretKey) -> PublicKey
where D: FixedOutput<OutputSize = U64> + Default + Input { where D: Digest<OutputSize = U64> + Default {
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];
let pk: [u8; 32]; let pk: [u8; 32];
let mut digest: &mut [u8; 32]; let mut digest: &mut [u8; 32];
h.digest(secret_key.as_bytes()); h.input(secret_key.as_bytes());
hash.copy_from_slice(h.fixed_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);
@ -306,7 +310,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: FixedOutput<OutputSize = U64> + Default + Input { where D: Digest<OutputSize = U64> + Default {
let mut h: D = D::default(); let mut h: D = D::default();
let mut a: ExtendedPoint; let mut a: ExtendedPoint;
@ -330,16 +334,16 @@ 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.digest(&bottom_half[..]); h.input(&bottom_half[..]);
h.digest(&self.to_bytes()); h.input(&self.to_bytes());
h.digest(&message); h.input(&message);
let digest_bytes = h.fixed_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 = &(&digest_reduced * &a) + &(&Scalar(*top_half) * &constants::ED25519_BASEPOINT); r = &(&digest_reduced * &a) + &(&Scalar(*top_half) * &constants::ED25519_BASEPOINT);
if arrays_equal_ct(bottom_half, &r.compress_edwards().to_bytes()) == 1 { if slices_equal(bottom_half, &r.compress_edwards().to_bytes()) == 1 {
return true return true
} else { } else {
return false return false
@ -416,7 +420,7 @@ impl Keypair {
/// Other suitable hash functions include Keccak-512 and Blake2b-512. /// Other suitable hash functions include Keccak-512 and Blake2b-512.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn generate<D>(csprng: &mut Rng) -> Keypair pub fn generate<D>(csprng: &mut Rng) -> Keypair
where D: FixedOutput<OutputSize = U64> + Default + Input { where D: Digest<OutputSize = U64> + Default {
let sk: SecretKey = SecretKey::generate(csprng); let sk: SecretKey = SecretKey::generate(csprng);
let pk: PublicKey = PublicKey::from_secret::<D>(&sk); let pk: PublicKey = PublicKey::from_secret::<D>(&sk);
@ -425,7 +429,7 @@ 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: FixedOutput<OutputSize = U64> + Default + Input { where D: Digest<OutputSize = U64> + Default {
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];
@ -440,7 +444,7 @@ impl Keypair {
let secret_key: &[u8; 32] = self.secret.as_bytes(); let secret_key: &[u8; 32] = self.secret.as_bytes();
let public_key: &[u8; 32] = self.public.as_bytes(); let public_key: &[u8; 32] = self.public.as_bytes();
h.digest(secret_key); h.input(secret_key);
hash.copy_from_slice(h.fixed_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));
@ -449,8 +453,8 @@ impl Keypair {
expanded_key_secret[31] |= 64; expanded_key_secret[31] |= 64;
h = D::default(); h = D::default();
h.digest(&hash[32..]); h.input(&hash[32..]);
h.digest(&message); h.input(&message);
hash.copy_from_slice(h.fixed_result().as_slice()); hash.copy_from_slice(h.fixed_result().as_slice());
mesg_digest = Scalar::reduce(&hash); mesg_digest = Scalar::reduce(&hash);
@ -458,9 +462,9 @@ impl Keypair {
r = &mesg_digest * &constants::ED25519_BASEPOINT; r = &mesg_digest * &constants::ED25519_BASEPOINT;
h = D::default(); h = D::default();
h.digest(&r.compress_edwards().to_bytes()[..]); h.input(&r.compress_edwards().to_bytes()[..]);
h.digest(public_key); h.input(public_key);
h.digest(&message); h.input(&message);
hash.copy_from_slice(h.fixed_result().as_slice()); hash.copy_from_slice(h.fixed_result().as_slice());
hram_digest = Scalar::reduce(&hash); hram_digest = Scalar::reduce(&hash);
@ -475,7 +479,7 @@ impl Keypair {
/// 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: FixedOutput<OutputSize = U64> + Default + Input { where D: FixedOutput<OutputSize = U64> + BlockInput + Default + Input {
self.public.verify::<D>(message, signature) self.public.verify::<D>(message, signature)
} }
} }

View file

@ -115,6 +115,7 @@ extern crate arrayref;
extern crate curve25519_dalek; extern crate curve25519_dalek;
extern crate generic_array; extern crate generic_array;
extern crate digest; extern crate digest;
extern crate subtle;
#[cfg(feature = "std")] #[cfg(feature = "std")]
extern crate rand; extern crate rand;