Skip to content

Commit

Permalink
Add EcdsaCurve trait impls (#1019)
Browse files Browse the repository at this point in the history
This is a marker trait added in RustCrypto/signatures#787 which marks an
elliptic curve as being intended for use with ECDSA.

It's the intended successor of the `SignPrimitive` and `VerifyPrimitive`
traits, carrying with it the one bit of information needed to eliminate
the need for custom impls: a flag for low-S normalization.

`bp256`, `bp384`, `k256`, `p192`, `p224`, `p256`, `p384`, and `p521`
have all received trait impls.

Of those, only `k256` has the `NORMALIZE_S` flag set to `true`.
  • Loading branch information
tarcieri authored Jan 17, 2024
1 parent 92ec009 commit 32e6310
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 14 deletions.
18 changes: 13 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io.ecdsa]
git = "https://github.com/rustcrypto/signatures.git"
4 changes: 4 additions & 0 deletions bp256/src/r1/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub type Signature = ecdsa::Signature<BrainpoolP256r1>;
/// ECDSA/brainpoolP256r1 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa::der::Signature<BrainpoolP256r1>;

impl ecdsa::EcdsaCurve for BrainpoolP256r1 {
const NORMALIZE_S: bool = false;
}

#[cfg(feature = "sha256")]
impl ecdsa::hazmat::DigestPrimitive for BrainpoolP256r1 {
type Digest = sha2::Sha256;
Expand Down
4 changes: 4 additions & 0 deletions bp256/src/t1/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub type Signature = ecdsa::Signature<BrainpoolP256t1>;
/// ECDSA/brainpoolP256t1 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa::der::Signature<BrainpoolP256t1>;

impl ecdsa::EcdsaCurve for BrainpoolP256t1 {
const NORMALIZE_S: bool = false;
}

#[cfg(feature = "sha256")]
impl ecdsa::hazmat::DigestPrimitive for BrainpoolP256t1 {
type Digest = sha2::Sha256;
Expand Down
4 changes: 4 additions & 0 deletions bp384/src/r1/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub type Signature = ecdsa::Signature<BrainpoolP384r1>;
/// ECDSA/brainpoolP384r1 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa::der::Signature<BrainpoolP384r1>;

impl ecdsa::EcdsaCurve for BrainpoolP384r1 {
const NORMALIZE_S: bool = false;
}

#[cfg(feature = "sha384")]
impl ecdsa::hazmat::DigestPrimitive for BrainpoolP384r1 {
type Digest = sha2::Sha384;
Expand Down
4 changes: 4 additions & 0 deletions bp384/src/t1/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub type Signature = ecdsa::Signature<BrainpoolP384t1>;
/// ECDSA/brainpoolP384t1 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa::der::Signature<BrainpoolP384t1>;

impl ecdsa::EcdsaCurve for BrainpoolP384t1 {
const NORMALIZE_S: bool = false;
}

#[cfg(feature = "sha384")]
impl ecdsa::hazmat::DigestPrimitive for BrainpoolP384t1 {
type Digest = sha2::Sha384;
Expand Down
6 changes: 5 additions & 1 deletion k256/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
pub use ecdsa_core::{
signature::{self, Error},
RecoveryId,
EcdsaCurve, RecoveryId,
};

#[cfg(any(feature = "ecdsa", feature = "sha256"))]
Expand All @@ -165,6 +165,10 @@ pub type Signature = ecdsa_core::Signature<Secp256k1>;
/// ECDSA/secp256k1 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa_core::der::Signature<Secp256k1>;

impl EcdsaCurve for Secp256k1 {
const NORMALIZE_S: bool = true;
}

/// ECDSA/secp256k1 signing key
#[cfg(feature = "ecdsa")]
pub type SigningKey = ecdsa_core::SigningKey<Secp256k1>;
Expand Down
10 changes: 8 additions & 2 deletions p192/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@
//! [NIST Special Publication 800-131A Revision 2]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
pub use ecdsa_core::signature::{self, Error};
#[cfg(feature = "ecdsa")]
use {crate::AffinePoint, ecdsa_core::hazmat::VerifyPrimitive};

use super::NistP192;
use ecdsa_core::EcdsaCurve;

#[cfg(feature = "ecdsa")]
use {crate::AffinePoint, ecdsa_core::hazmat::VerifyPrimitive};

/// ECDSA/P-192 signature (fixed-size)
pub type Signature = ecdsa_core::Signature<NistP192>;

/// ECDSA/P-192 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa_core::der::Signature<NistP192>;

impl EcdsaCurve for NistP192 {
const NORMALIZE_S: bool = false;
}

/// ECDSA/P-192 verification key (i.e. public key)
#[cfg(feature = "ecdsa")]
pub type VerifyingKey = ecdsa_core::VerifyingKey<NistP192>;
Expand Down
10 changes: 8 additions & 2 deletions p224/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,26 @@
//! ```
pub use ecdsa_core::signature::{self, Error};

use super::NistP224;
use ecdsa_core::EcdsaCurve;

#[cfg(feature = "ecdsa")]
use {
crate::{AffinePoint, Scalar},
ecdsa_core::hazmat::{SignPrimitive, VerifyPrimitive},
};

use super::NistP224;

/// ECDSA/P-224 signature (fixed-size)
pub type Signature = ecdsa_core::Signature<NistP224>;

/// ECDSA/P-224 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa_core::der::Signature<NistP224>;

impl EcdsaCurve for NistP224 {
const NORMALIZE_S: bool = false;
}

/// ECDSA/P-224 signing key
#[cfg(feature = "ecdsa")]
pub type SigningKey = ecdsa_core::SigningKey<NistP224>;
Expand Down
5 changes: 5 additions & 0 deletions p256/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
pub use ecdsa_core::signature::{self, Error};

use super::NistP256;
use ecdsa_core::EcdsaCurve;

#[cfg(feature = "ecdsa")]
use {
Expand All @@ -55,6 +56,10 @@ pub type Signature = ecdsa_core::Signature<NistP256>;
/// ECDSA/P-256 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa_core::der::Signature<NistP256>;

impl EcdsaCurve for NistP256 {
const NORMALIZE_S: bool = false;
}

/// ECDSA/P-256 signing key
#[cfg(feature = "ecdsa")]
pub type SigningKey = ecdsa_core::SigningKey<NistP256>;
Expand Down
10 changes: 8 additions & 2 deletions p384/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,26 @@
//! ```
pub use ecdsa_core::signature::{self, Error};

use super::NistP384;
use ecdsa_core::EcdsaCurve;

#[cfg(feature = "ecdsa")]
use {
crate::{AffinePoint, Scalar},
ecdsa_core::hazmat::{SignPrimitive, VerifyPrimitive},
};

use super::NistP384;

/// ECDSA/P-384 signature (fixed-size)
pub type Signature = ecdsa_core::Signature<NistP384>;

/// ECDSA/P-384 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa_core::der::Signature<NistP384>;

impl EcdsaCurve for NistP384 {
const NORMALIZE_S: bool = false;
}

/// ECDSA/P-384 signing key
#[cfg(feature = "ecdsa")]
pub type SigningKey = ecdsa_core::SigningKey<NistP384>;
Expand Down
10 changes: 8 additions & 2 deletions p521/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,26 @@
//! ```
pub use ecdsa_core::signature::{self, Error};

use super::NistP521;
use ecdsa_core::EcdsaCurve;

#[cfg(feature = "ecdsa")]
use {
crate::{AffinePoint, Scalar},
ecdsa_core::hazmat::{SignPrimitive, VerifyPrimitive},
};

use super::NistP521;

/// ECDSA/P-521 signature (fixed-size)
pub type Signature = ecdsa_core::Signature<NistP521>;

/// ECDSA/P-521 signature (ASN.1 DER encoded)
pub type DerSignature = ecdsa_core::der::Signature<NistP521>;

impl EcdsaCurve for NistP521 {
const NORMALIZE_S: bool = false;
}

/// ECDSA/P-521 signing key
#[cfg(feature = "ecdsa")]
pub type SigningKey = ecdsa_core::SigningKey<NistP521>;
Expand Down

0 comments on commit 32e6310

Please sign in to comment.