diff --git a/Cargo.lock b/Cargo.lock index 5c29b4cf..60912e27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -45,7 +45,7 @@ dependencies = [ [[package]] name = "elliptic-curve" version = "0.5.0-pre" -source = "git+https://github.com/RustCrypto/traits#3e7b861581185d7350db3315cd1a99b4c6d382e4" +source = "git+https://github.com/RustCrypto/traits#3fd8ac7fa787cfbea275306fab011a592206b1b2" dependencies = [ "generic-array", "subtle", diff --git a/ecdsa/src/asn1.rs b/ecdsa/src/asn1.rs index 88e09d7b..952e4e9a 100644 --- a/ecdsa/src/asn1.rs +++ b/ecdsa/src/asn1.rs @@ -7,7 +7,7 @@ // use crate::{ - generic_array::{ArrayLength, GenericArray}, + generic_array::{typenum::Unsigned, ArrayLength, GenericArray}, Error, }; use core::{ @@ -15,7 +15,7 @@ use core::{ fmt, ops::{Add, Range}, }; -use elliptic_curve::{consts::U9, ScalarBytes}; +use elliptic_curve::{consts::U9, weierstrass::Curve, ScalarBytes}; /// Maximum overhead of an ASN.1 DER-encoded ECDSA signature for a given curve: /// 9-bytes. @@ -34,10 +34,11 @@ use elliptic_curve::{consts::U9, ScalarBytes}; pub type MaxOverhead = U9; /// Maximum size of an ASN.1 DER encoded signature for the given elliptic curve. -pub type MaxSize = <::Output as Add>::Output; +pub type MaxSize = + <<::ElementSize as Add>::Output as Add>::Output; /// Byte array containing a serialized ASN.1 signature -type DocumentBytes = GenericArray>; +type DocumentBytes = GenericArray>; /// ASN.1 `INTEGER` tag const INTEGER_TAG: u8 = 0x02; @@ -48,14 +49,15 @@ const SEQUENCE_TAG: u8 = 0x30; /// ASN.1 DER-encoded signature. /// /// Generic over the scalar size of the elliptic curve. -pub struct Signature +pub struct Signature where - ScalarSize: Add + ArrayLength, - MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + C: Curve, + C::ElementSize: Add + ArrayLength, + MaxSize: ArrayLength, + ::Output: Add + ArrayLength, { /// ASN.1 DER-encoded signature data - bytes: DocumentBytes, + bytes: DocumentBytes, /// Range of the `r` value within the signature r_range: Range, @@ -64,11 +66,12 @@ where s_range: Range, } -impl signature::Signature for Signature +impl signature::Signature for Signature where - ScalarSize: Add + ArrayLength, - MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + C: Curve, + C::ElementSize: Add + ArrayLength, + MaxSize: ArrayLength, + ::Output: Add + ArrayLength, { /// Parse an ASN.1 DER-encoded ECDSA signature from a byte slice fn from_bytes(bytes: &[u8]) -> Result { @@ -77,11 +80,12 @@ where } #[allow(clippy::len_without_is_empty)] -impl Signature +impl Signature where - ScalarSize: Add + ArrayLength, - MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + C: Curve, + C::ElementSize: Add + ArrayLength, + MaxSize: ArrayLength, + ::Output: Add + ArrayLength, { /// Get the length of the signature in bytes pub fn len(&self) -> usize { @@ -89,11 +93,11 @@ where } /// Create an ASN.1 DER encoded signature from the `r` and `s` scalars - pub(crate) fn from_scalars(r: &ScalarBytes, s: &ScalarBytes) -> Self { + pub(crate) fn from_scalars(r: &ScalarBytes, s: &ScalarBytes) -> Self { let r_len = int_length(r); let s_len = int_length(s); - let scalar_size = ScalarSize::to_usize(); - let mut bytes = DocumentBytes::::default(); + let scalar_size = C::ElementSize::to_usize(); + let mut bytes = DocumentBytes::::default(); // SEQUENCE header bytes[0] = SEQUENCE_TAG as u8; @@ -132,22 +136,24 @@ where } } -impl AsRef<[u8]> for Signature +impl AsRef<[u8]> for Signature where - ScalarSize: Add + ArrayLength, - MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + C: Curve, + C::ElementSize: Add + ArrayLength, + MaxSize: ArrayLength, + ::Output: Add + ArrayLength, { fn as_ref(&self) -> &[u8] { &self.bytes.as_slice()[..self.len()] } } -impl fmt::Debug for Signature +impl fmt::Debug for Signature where - ScalarSize: Add + ArrayLength, - MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + C: Curve, + C::ElementSize: Add + ArrayLength, + MaxSize: ArrayLength, + ::Output: Add + ArrayLength, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("asn1::Signature") @@ -157,11 +163,12 @@ where } } -impl TryFrom<&[u8]> for Signature +impl TryFrom<&[u8]> for Signature where - ScalarSize: Add + ArrayLength, - MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + C: Curve, + C::ElementSize: Add + ArrayLength, + MaxSize: ArrayLength, + ::Output: Add + ArrayLength, { type Error = Error; @@ -198,12 +205,12 @@ where } // First INTEGER (r) - let r_range = parse_int(&bytes[offset..], ScalarSize::to_usize())?; + let r_range = parse_int(&bytes[offset..], C::ElementSize::to_usize())?; let r_start = offset.checked_add(r_range.start).unwrap(); let r_end = offset.checked_add(r_range.end).unwrap(); // Second INTEGER (s) - let s_range = parse_int(&bytes[r_end..], ScalarSize::to_usize())?; + let s_range = parse_int(&bytes[r_end..], C::ElementSize::to_usize())?; let s_start = r_end.checked_add(s_range.start).unwrap(); let s_end = r_end.checked_add(s_range.end).unwrap(); @@ -211,7 +218,7 @@ where return Err(Error::new()); } - let mut byte_arr = DocumentBytes::::default(); + let mut byte_arr = DocumentBytes::::default(); byte_arr[..s_end].copy_from_slice(bytes.as_ref()); Ok(Signature { @@ -311,16 +318,18 @@ fn trim_zeroes(mut bytes: &[u8], scalar_size: usize) -> Result { #[cfg(test)] mod tests { - use elliptic_curve::{consts::U32, weierstrass::Curve}; + use elliptic_curve::consts::U32; use signature::Signature as _; #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)] pub struct ExampleCurve; - impl Curve for ExampleCurve { - type ScalarSize = U32; + impl elliptic_curve::Curve for ExampleCurve { + type ElementSize = U32; } + impl elliptic_curve::weierstrass::Curve for ExampleCurve {} + type Signature = crate::Signature; const EXAMPLE_SIGNATURE: [u8; 64] = [ diff --git a/ecdsa/src/hazmat.rs b/ecdsa/src/hazmat.rs index a6af0011..b334224a 100644 --- a/ecdsa/src/hazmat.rs +++ b/ecdsa/src/hazmat.rs @@ -43,7 +43,7 @@ where &self, ephemeral_scalar: &Self::Scalar, masking_scalar: Option<&Self::Scalar>, - hashed_msg: &ScalarBytes, + hashed_msg: &ScalarBytes, ) -> Result, Error>; } @@ -66,7 +66,7 @@ where /// - `signature`: signature to be verified against the key and message fn verify_prehashed( &self, - hashed_msg: &ScalarBytes, + hashed_msg: &ScalarBytes, signature: &Signature, ) -> Result<(), Error>; } @@ -92,7 +92,7 @@ pub trait DigestPrimitive: Curve { #[cfg(feature = "digest")] impl PrehashSignature for Signature where - ::Output: ArrayLength, + ::Output: ArrayLength, { type Digest = C::Digest; } diff --git a/ecdsa/src/lib.rs b/ecdsa/src/lib.rs index 99596ef4..7fc6e583 100644 --- a/ecdsa/src/lib.rs +++ b/ecdsa/src/lib.rs @@ -52,7 +52,7 @@ use elliptic_curve::ScalarBytes; use generic_array::{typenum::Unsigned, ArrayLength, GenericArray}; /// Size of a fixed sized signature for the given elliptic curve. -pub type SignatureSize = <::ScalarSize as Add>::Output; +pub type SignatureSize = <::ElementSize as Add>::Output; /// Fixed-size byte array containing an ECDSA signature pub type SignatureBytes = GenericArray>; @@ -86,9 +86,9 @@ where SignatureSize: ArrayLength, { /// Create a [`Signature`] from the serialized `r` and `s` components - pub fn from_scalars(r: &ScalarBytes, s: &ScalarBytes) -> Self { + pub fn from_scalars(r: &ScalarBytes, s: &ScalarBytes) -> Self { let mut bytes = SignatureBytes::::default(); - let scalar_size = C::ScalarSize::to_usize(); + let scalar_size = C::ElementSize::to_usize(); bytes[..scalar_size].copy_from_slice(r.as_slice()); bytes[scalar_size..].copy_from_slice(s.as_slice()); Signature { bytes } @@ -97,31 +97,31 @@ where /// Parse a signature from ASN.1 DER pub fn from_asn1(bytes: &[u8]) -> Result where - C::ScalarSize: Add + ArrayLength, - asn1::MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + C::ElementSize: Add + ArrayLength, + asn1::MaxSize: ArrayLength, + ::Output: Add + ArrayLength, { - asn1::Signature::::try_from(bytes).map(Into::into) + asn1::Signature::::try_from(bytes).map(Into::into) } /// Serialize this signature as ASN.1 DER - pub fn to_asn1(&self) -> asn1::Signature + pub fn to_asn1(&self) -> asn1::Signature where - C::ScalarSize: Add + ArrayLength, - asn1::MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + C::ElementSize: Add + ArrayLength, + asn1::MaxSize: ArrayLength, + ::Output: Add + ArrayLength, { asn1::Signature::from_scalars(self.r(), self.s()) } /// Get the `r` component of this signature - pub fn r(&self) -> &ScalarBytes { - ScalarBytes::from_slice(&self.bytes[..C::ScalarSize::to_usize()]) + pub fn r(&self) -> &ScalarBytes { + ScalarBytes::::from_slice(&self.bytes[..C::ElementSize::to_usize()]) } /// Get the `s` component of this signature - pub fn s(&self) -> &ScalarBytes { - ScalarBytes::from_slice(&self.bytes[C::ScalarSize::to_usize()..]) + pub fn s(&self) -> &ScalarBytes { + ScalarBytes::::from_slice(&self.bytes[C::ElementSize::to_usize()..]) } } @@ -174,16 +174,16 @@ where } } -impl From> for Signature +impl From> for Signature where C: Curve, - C::ScalarSize: Add + ArrayLength, - asn1::MaxSize: ArrayLength, - ::Output: Add + ArrayLength, + C::ElementSize: Add + ArrayLength, + asn1::MaxSize: ArrayLength, + ::Output: Add + ArrayLength, { - fn from(doc: asn1::Signature) -> Signature { + fn from(doc: asn1::Signature) -> Signature { let mut bytes = SignatureBytes::::default(); - let scalar_size = C::ScalarSize::to_usize(); + let scalar_size = C::ElementSize::to_usize(); let r_begin = scalar_size.checked_sub(doc.r().len()).unwrap(); let s_begin = bytes.len().checked_sub(doc.s().len()).unwrap();