Struct yubihsm::ecdsa::Signature

source ·
pub struct Signature<C>where
    C: PrimeCurve,{ /* private fields */ }
Expand description

ECDSA signature (fixed-size). Generic over elliptic curve types.

Serialized as fixed-sized big endian scalar values with no added framing:

  • r: field element size for the given curve, big-endian
  • s: field element size for the given curve, big-endian

Both r and s MUST be non-zero.

For example, in a curve with a 256-bit modulus like NIST P-256 or secp256k1, r and s will both be 32-bytes and serialized as big endian, resulting in a signature with a total of 64-bytes.

ASN.1 DER-encoded signatures also supported via the Signature::from_der and Signature::to_der methods.

serde support

When the serde feature of this crate is enabled, it provides support for serializing and deserializing ECDSA signatures using the Serialize and Deserialize traits.

The serialization uses a hexadecimal encoding when used with “human readable” text formats, and a binary encoding otherwise.

Implementations§

source§

impl<C> Signature<C>where C: PrimeCurve, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

source

pub fn from_bytes( bytes: &GenericArray<u8, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output> ) -> Result<Signature<C>, Error>

Parse a signature from fixed-width bytes, i.e. 2 * the size of [FieldBytes] for a particular curve.

Returns
  • Ok(signature) if the r and s components are both in the valid range 1..n when serialized as concatenated big endian integers.
  • Err(err) if the r and/or s component of the signature is out-of-range when interpreted as a big endian integer.
source

pub fn from_slice(slice: &[u8]) -> Result<Signature<C>, Error>

Parse a signature from a byte slice.

source

pub fn from_der(bytes: &[u8]) -> Result<Signature<C>, Error>where <<<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output as Add<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>>>::Output: ArrayLength<u8>, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: Add<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>> + ArrayLength<u8>,

Available on crate feature der only.

Parse a signature from ASN.1 DER.

source

pub fn from_scalars( r: impl Into<GenericArray<u8, <C as Curve>::FieldBytesSize>>, s: impl Into<GenericArray<u8, <C as Curve>::FieldBytesSize>> ) -> Result<Signature<C>, Error>

Create a Signature from the serialized r and s scalar values which comprise the signature.

Returns
  • Ok(signature) if the r and s components are both in the valid range 1..n when serialized as concatenated big endian integers.
  • Err(err) if the r and/or s component of the signature is out-of-range when interpreted as a big endian integer.
source

pub fn split_bytes( &self ) -> (GenericArray<u8, <C as Curve>::FieldBytesSize>, GenericArray<u8, <C as Curve>::FieldBytesSize>)

Split the signature into its r and s components, represented as bytes.

source

pub fn to_bytes( &self ) -> GenericArray<u8, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output>

Serialize this signature as bytes.

source

pub fn to_der(&self) -> Signature<C>where <<<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output as Add<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>>>::Output: ArrayLength<u8>, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: Add<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>> + ArrayLength<u8>,

Available on crate feature der only.

Serialize this signature as ASN.1 DER.

source

pub fn to_vec(&self) -> Vec<u8, Global>

Available on crate feature alloc only.

Convert this signature into a byte vector.

source§

impl<C> Signature<C>where C: PrimeCurve + CurveArithmetic, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

source

pub fn r(&self) -> NonZeroScalar<C>

Available on crate feature arithmetic only.

Get the r component of this signature

source

pub fn s(&self) -> NonZeroScalar<C>

Available on crate feature arithmetic only.

Get the s component of this signature

source

pub fn split_scalars(&self) -> (NonZeroScalar<C>, NonZeroScalar<C>)

Available on crate feature arithmetic only.

Split the signature into its r and s scalars.

source

pub fn normalize_s(&self) -> Option<Signature<C>>

Available on crate feature arithmetic only.

Normalize signature into “low S” form as described in BIP 0062: Dealing with Malleability.

Trait Implementations§

source§

impl<C> AssociatedAlgorithmIdentifier for Signature<C>where C: PrimeCurve, Signature<C>: AssociatedOid,

Available on crate feature pkcs8 only.

ECDSA AlgorithmIdentifier which identifies the digest used by default with the Signer and Verifier traits.

§

type Params = AnyRef<'static>

Algorithm parameters.
source§

const ALGORITHM_IDENTIFIER: AlgorithmIdentifier<AnyRef<'static>> = AlgorithmIdentifierRef{ oid: Self::OID, parameters: None,}

AlgorithmIdentifier for this structure.
source§

impl<C> AssociatedOid for Signature<C>where C: DigestPrimitive, <C as DigestPrimitive>::Digest: AssociatedOid,

Available on crate features digest and hazmat only.

ECDSA [ObjectIdentifier] which identifies the digest used by default with the Signer and Verifier traits.

To support non-default digest algorithms, use the SignatureWithOid type instead.

source§

const OID: ObjectIdentifier = match ecdsa_oid_for_digest(<C::Digest>::OID) { Some(oid) => oid, None => { ::core::panicking::panic_fmt(<#[lang = "format_arguments"]>::new_const(&["no RFC5758 ECDSA OID defined for DigestPrimitive::Digest"])); } }

The OID associated with this type.
source§

impl<C> Clone for Signature<C>where C: Clone + PrimeCurve,

source§

fn clone(&self) -> Signature<C>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<C> Debug for Signature<C>where C: PrimeCurve, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<D> DigestSigner<D, Signature<NistP256>> for Signer<NistP256>where D: Digest<OutputSize = U32> + Default,

source§

fn try_sign_digest(&self, digest: D) -> Result<Signature<NistP256>, Error>

Compute a fixed-sized P-256 ECDSA signature of the given digest

source§

fn sign_digest(&self, digest: D) -> S

Sign the given prehashed message Digest, returning a signature. Read more
source§

impl<D> DigestSigner<D, Signature<NistP384>> for Signer<NistP384>where D: Digest<OutputSize = U32> + Default,

source§

fn try_sign_digest(&self, digest: D) -> Result<Signature<NistP384>, Error>

Compute a fixed-sized P-384 ECDSA signature of the given digest

source§

fn sign_digest(&self, digest: D) -> S

Sign the given prehashed message Digest, returning a signature. Read more
source§

impl<D> DigestSigner<D, Signature<Secp256k1>> for Signer<Secp256k1>where D: Digest<OutputSize = U32> + Default,

Available on crate feature secp256k1 only.
source§

fn try_sign_digest(&self, digest: D) -> Result<Signature<Secp256k1>, Error>

Compute a fixed-size secp256k1 ECDSA signature of the given digest

source§

fn sign_digest(&self, digest: D) -> S

Sign the given prehashed message Digest, returning a signature. Read more
source§

impl<C> Display for Signature<C>where C: PrimeCurve, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<C> From<Signature<C>> for Signature<C>where C: PrimeCurve, <<<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output as Add<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>>>::Output: ArrayLength<u8>, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: Add<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>> + ArrayLength<u8>,

source§

fn from(sig: Signature<C>) -> Signature<C>

Converts to this type from the input type.
source§

impl<C> From<SignatureWithOid<C>> for Signature<C>where C: PrimeCurve,

Available on crate feature digest only.
source§

fn from(sig: SignatureWithOid<C>) -> Signature<C>

Converts to this type from the input type.
source§

impl<C> FromStr for Signature<C>where C: PrimeCurve + CurveArithmetic, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

Available on crate feature arithmetic only.
§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(hex: &str) -> Result<Signature<C>, Error>

Parses a string s to return a value of this type. Read more
source§

impl<C> LowerHex for Signature<C>where C: PrimeCurve, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter.
source§

impl<C> PartialEq<Signature<C>> for Signature<C>where C: PartialEq<C> + PrimeCurve,

source§

fn eq(&self, other: &Signature<C>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<C> PrehashSignature for Signature<C>where C: DigestPrimitive, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

Available on crate feature digest only.
§

type Digest = <C as DigestPrimitive>::Digest

Preferred Digest algorithm to use when computing this signature type.
source§

impl PrehashSigner<Signature<NistP256>> for Signer<NistP256>

source§

fn sign_prehash(&self, prehash: &[u8]) -> Result<Signature<NistP256>, Error>

Compute a fixed-size P-256 ECDSA signature of a digest output.

source§

impl PrehashSigner<Signature<NistP384>> for Signer<NistP384>

source§

fn sign_prehash(&self, prehash: &[u8]) -> Result<Signature<NistP384>, Error>

Compute a fixed-size P-384 ECDSA signature of a digest output.

source§

impl PrehashSigner<Signature<Secp256k1>> for Signer<Secp256k1>

Available on crate feature secp256k1 only.
source§

fn sign_prehash(&self, prehash: &[u8]) -> Result<Signature<Secp256k1>, Error>

Attempt to sign the given message digest, returning a digital signature on success, or an error if something went wrong. Read more
source§

impl<C> SignatureEncoding for Signature<C>where C: PrimeCurve, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

§

type Repr = GenericArray<u8, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output>

Byte representation of a signature.
source§

fn to_bytes(&self) -> Self::Repr

Encode signature as its byte representation.
source§

fn to_vec(&self) -> Vec<u8, Global>

Available on crate feature alloc only.
Encode signature as a byte vector.
source§

fn encoded_len(&self) -> usize

Get the length of this signature when encoded.
source§

impl<C> TryFrom<&[u8]> for Signature<C>where C: PrimeCurve, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(slice: &[u8]) -> Result<Signature<C>, Error>

Performs the conversion.
source§

impl<C> TryFrom<Signature<C>> for Signature<C>where C: PrimeCurve, <<<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output as Add<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>>>::Output: ArrayLength<u8>, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: Add<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>> + ArrayLength<u8>,

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(sig: Signature<C>) -> Result<Signature<C>, Error>

Performs the conversion.
source§

impl<C> UpperHex for Signature<C>where C: PrimeCurve, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter.
source§

impl<C> Copy for Signature<C>where C: PrimeCurve, <<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output: ArrayLength<u8>, <<<C as Curve>::FieldBytesSize as Add<<C as Curve>::FieldBytesSize>>::Output as ArrayLength<u8>>::ArrayType: Copy,

source§

impl<C> Eq for Signature<C>where C: Eq + PrimeCurve,

source§

impl<C> StructuralEq for Signature<C>where C: PrimeCurve,

source§

impl<C> StructuralPartialEq for Signature<C>where C: PrimeCurve,

Auto Trait Implementations§

§

impl<C> RefUnwindSafe for Signature<C>where <C as Curve>::Uint: RefUnwindSafe,

§

impl<C> Send for Signature<C>

§

impl<C> Sync for Signature<C>

§

impl<C> Unpin for Signature<C>where <C as Curve>::Uint: Unpin,

§

impl<C> UnwindSafe for Signature<C>where <C as Curve>::Uint: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> DynAssociatedAlgorithmIdentifier for Twhere T: AssociatedAlgorithmIdentifier,

§

fn algorithm_identifier(&self) -> Result<AlgorithmIdentifier<Any>, Error>

AlgorithmIdentifier for this structure.
§

impl<T> DynAssociatedOid for Twhere T: AssociatedOid,

§

fn oid(&self) -> ObjectIdentifier

Get the OID associated with this value.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.