Skip to content

Commit

Permalink
Update elliptic-curve and digest
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jan 17, 2022
1 parent c525b5c commit 38c3786
Show file tree
Hide file tree
Showing 10 changed files with 760 additions and 469 deletions.
475 changes: 30 additions & 445 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
resolver = "2"
members = [
"ecdsa",
"ed25519",
"rfc6979"
]
10 changes: 5 additions & 5 deletions ecdsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ edition = "2021"
rust-version = "1.56"

[dependencies]
elliptic-curve = { version = "0.11.6", default-features = false, features = ["sec1"] }
signature = { version = ">= 1.3.1, <1.5", default-features = false, features = ["rand-preview"] }
elliptic-curve = { git = "https://github.com/khonsulabs/traits", branch = "digest", default-features = false, features = ["sec1"] }
signature = { version = "1.5", default-features = false, features = ["rand-preview"] }

# optional dependencies
der = { version = "0.5", optional = true }
rfc6979 = { version = "0.1", optional = true, path = "../rfc6979" }
rfc6979 = { git = "https://github.com/khonsulabs/signatures", branch = "elliptic-curve-digest", optional = true }

[dev-dependencies]
elliptic-curve = { version = "0.11.6", default-features = false, features = ["dev"] }
elliptic-curve = { git = "https://github.com/khonsulabs/traits", branch = "digest", default-features = false, features = ["dev"] }
hex-literal = "0.3"
sha2 = { version = "0.9", default-features = false }
sha2 = { version = "0.10", default-features = false }

[features]
default = ["digest"]
Expand Down
18 changes: 16 additions & 2 deletions ecdsa/src/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ use crate::{
#[cfg(all(feature = "sign"))]
use {
elliptic_curve::{ff::PrimeField, zeroize::Zeroizing, NonZeroScalar, ScalarCore},
signature::digest::{BlockInput, FixedOutput, Reset, Update},
signature::digest::{
block_buffer::Eager,
core_api::{BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore},
generic_array::typenum::{IsLess, Le, NonZero, U256},
HashMarker, OutputSizeUser,
},
};

/// Try to sign the given prehashed message using ECDSA.
Expand Down Expand Up @@ -187,7 +192,16 @@ pub fn rfc6979_generate_k<C, D>(
) -> Zeroizing<NonZeroScalar<C>>
where
C: PrimeCurve + ProjectiveArithmetic,
D: FixedOutput<OutputSize = FieldSize<C>> + BlockInput + Clone + Default + Reset + Update,
D: CoreProxy + OutputSizeUser<OutputSize = FieldSize<C>>,
D::Core: BlockSizeUser
+ BufferKindUser<BufferKind = Eager>
+ Clone
+ Default
+ FixedOutputCore
+ HashMarker
+ OutputSizeUser<OutputSize = D::OutputSize>,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
// TODO(tarcieri): avoid this conversion
let x = Zeroizing::new(ScalarCore::<C>::from(x));
Expand Down
37 changes: 30 additions & 7 deletions ecdsa/src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ use elliptic_curve::{
FieldBytes, FieldSize, NonZeroScalar, PrimeCurve, ProjectiveArithmetic, Scalar, SecretKey,
};
use signature::{
digest::{BlockInput, Digest, FixedOutput, Reset, Update},
digest::{
block_buffer::Eager,
core_api::{BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore},
generic_array::typenum::{IsLess, Le, NonZero, U256},
Digest, HashMarker, OutputSizeUser,
},
rand_core::{CryptoRng, RngCore},
DigestSigner, RandomizedDigestSigner, RandomizedSigner, Signer,
};
Expand Down Expand Up @@ -168,15 +173,24 @@ where
impl<C, D> DigestSigner<D, Signature<C>> for SigningKey<C>
where
C: PrimeCurve + ProjectiveArithmetic,
D: FixedOutput<OutputSize = FieldSize<C>> + BlockInput + Clone + Default + Reset + Update,
D: CoreProxy + Digest + OutputSizeUser<OutputSize = FieldSize<C>>,
D::Core: BlockSizeUser
+ BufferKindUser<BufferKind = Eager>
+ Clone
+ Default
+ FixedOutputCore
+ HashMarker
+ OutputSizeUser<OutputSize = D::OutputSize>,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
Scalar<C>: Invert<Output = Scalar<C>> + Reduce<C::UInt> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
/// Sign message prehash using a deterministic ephemeral scalar (`k`)
/// computed using the algorithm described in RFC 6979 (Section 3.2):
/// <https://tools.ietf.org/html/rfc6979#section-3>
fn try_sign_digest(&self, msg_digest: D) -> Result<Signature<C>> {
let msg_scalar = Scalar::<C>::from_be_bytes_reduced(msg_digest.finalize_fixed());
let msg_scalar = Scalar::<C>::from_be_bytes_reduced(msg_digest.finalize());
let k = rfc6979_generate_k::<C, D>(&self.inner, &msg_scalar, &[]);
Ok(self.inner.try_sign_prehashed(**k, msg_scalar)?.0)
}
Expand All @@ -190,14 +204,23 @@ where
SignatureSize<C>: ArrayLength<u8>,
{
fn try_sign(&self, msg: &[u8]) -> Result<Signature<C>> {
self.try_sign_digest(C::Digest::new().chain(msg))
self.try_sign_digest(C::Digest::new().chain_update(msg))
}
}

impl<C, D> RandomizedDigestSigner<D, Signature<C>> for SigningKey<C>
where
C: PrimeCurve + ProjectiveArithmetic,
D: FixedOutput<OutputSize = FieldSize<C>> + BlockInput + Clone + Default + Reset + Update,
D: CoreProxy + Digest + OutputSizeUser<OutputSize = FieldSize<C>>,
D::Core: BlockSizeUser
+ BufferKindUser<BufferKind = Eager>
+ Clone
+ Default
+ FixedOutputCore
+ HashMarker
+ OutputSizeUser<OutputSize = D::OutputSize>,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
Scalar<C>: Invert<Output = Scalar<C>> + Reduce<C::UInt> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
Expand All @@ -212,7 +235,7 @@ where
let mut entropy = FieldBytes::<C>::default();
rng.fill_bytes(&mut entropy);

let msg_scalar = Scalar::<C>::from_be_bytes_reduced(msg_digest.finalize_fixed());
let msg_scalar = Scalar::<C>::from_be_bytes_reduced(msg_digest.finalize());
let k = rfc6979_generate_k::<C, D>(&self.inner, &msg_scalar, &entropy);
Ok(self.inner.try_sign_prehashed(**k, msg_scalar)?.0)
}
Expand All @@ -226,7 +249,7 @@ where
SignatureSize<C>: ArrayLength<u8>,
{
fn try_sign_with_rng(&self, rng: impl CryptoRng + RngCore, msg: &[u8]) -> Result<Signature<C>> {
self.try_sign_digest_with_rng(rng, C::Digest::new().chain(msg))
self.try_sign_digest_with_rng(rng, C::Digest::new().chain_update(msg))
}
}

Expand Down
2 changes: 1 addition & 1 deletion ecdsa/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
SignatureSize<C>: ArrayLength<u8>,
{
fn verify(&self, msg: &[u8], signature: &Signature<C>) -> Result<()> {
self.verify_digest(C::Digest::new().chain(msg), signature)
self.verify_digest(C::Digest::new().chain_update(msg), signature)
}
}

Expand Down
Loading

0 comments on commit 38c3786

Please sign in to comment.