Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ecdsa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ jobs:
target: ${{ matrix.target }}
override: true
- run: cargo build --no-default-features --release --target ${{ matrix.target }}
- run: cargo build --no-default-features --features signer --release --target ${{ matrix.target }}
- run: cargo build --no-default-features --features verifier --release --target ${{ matrix.target }}
- run: cargo build --no-default-features --features sign --release --target ${{ matrix.target }}
- run: cargo build --no-default-features --features verify --release --target ${{ matrix.target }}

test:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions ecdsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ dev = ["digest", "zeroize"]
digest = ["elliptic-curve/digest", "signature/digest-preview"]
hazmat = []
rand = ["elliptic-curve/rand", "signature/rand-preview"]
signer = ["digest", "hazmat", "hmac", "zeroize"]
sign = ["digest", "hazmat", "hmac", "zeroize"]
std = ["elliptic-curve/std", "signature/std"]
verifier = ["digest", "hazmat"]
verify = ["digest", "hazmat"]
zeroize = ["elliptic-curve/zeroize"]

[package.metadata.docs.rs]
Expand Down
24 changes: 14 additions & 10 deletions ecdsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,29 @@ pub mod dev;
#[cfg_attr(docsrs, doc(cfg(feature = "hazmat")))]
pub mod hazmat;

#[cfg(feature = "signer")]
#[cfg_attr(docsrs, doc(cfg(feature = "signer")))]
pub mod signer;
#[cfg(feature = "sign")]
#[cfg_attr(docsrs, doc(cfg(feature = "sign")))]
pub mod rfc6979;

#[cfg(feature = "verifier")]
#[cfg_attr(docsrs, doc(cfg(feature = "verifier")))]
pub mod verifier;
#[cfg(feature = "sign")]
#[cfg_attr(docsrs, doc(cfg(feature = "sign")))]
pub mod sign;

#[cfg(feature = "verify")]
#[cfg_attr(docsrs, doc(cfg(feature = "verify")))]
pub mod verify;

// Re-export the `elliptic-curve` crate (and select types)
pub use elliptic_curve::{self, generic_array, sec1::EncodedPoint, weierstrass::Curve, SecretKey};

// Re-export the `signature` crate (and select types)
pub use signature::{self, Error};

#[cfg(feature = "signer")]
pub use signer::Signer;
#[cfg(feature = "sign")]
pub use sign::SigningKey;

#[cfg(feature = "verifier")]
pub use verifier::Verifier;
#[cfg(feature = "verify")]
pub use verify::VerifyKey;

use core::{
convert::TryFrom,
Expand Down
File renamed without changes.
38 changes: 18 additions & 20 deletions ecdsa/src/signer.rs → ecdsa/src/sign.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! ECDSA signer. Generic over elliptic curves.
//! ECDSA signing key. Generic over elliptic curves.
//!
//! Requires an [`elliptic_curve::Arithmetic`] impl on the curve, and a
//! [`SignPrimitive`] impl on its associated `Scalar` type.

// TODO(tarcieri): support for hardware crypto accelerators

pub mod rfc6979;

use crate::{
hazmat::{DigestPrimitive, SignPrimitive},
Error, Signature, SignatureSize,
rfc6979, Error, Signature, SignatureSize,
};
use core::convert::TryInto;
use elliptic_curve::{
Expand All @@ -30,8 +28,8 @@ use {
},
};

/// ECDSA signer
pub struct Signer<C>
/// ECDSA signing key
pub struct SigningKey<C>
where
C: Curve + Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
Expand All @@ -40,15 +38,15 @@ where
secret_scalar: NonZeroScalar<C>,
}

impl<C> Signer<C>
impl<C> SigningKey<C>
where
C: Curve + Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
SignatureSize<C>: ArrayLength<u8>,
{
/// Initialize signer from a raw scalar serialized as a byte slice
/// Initialize signing key from a raw scalar serialized as a byte slice.
// TODO(tarcieri): PKCS#8 support
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
pub fn new(bytes: &[u8]) -> Result<Self, Error> {
let scalar = bytes
.try_into()
.map(NonZeroScalar::from_bytes)
Expand All @@ -62,14 +60,14 @@ where
}
}

/// Create a new signer
// TODO(tarcieri): infallible conversion from a secret key
pub fn new(secret_key: &SecretKey<C>) -> Result<Self, Error> {
Self::from_bytes(secret_key.as_bytes())
/// Create a new signing key from a [`SecretKey`].
// TODO(tarcieri): infallible `From` conversion from a secret key
pub fn from_secret_key(secret_key: &SecretKey<C>) -> Result<Self, Error> {
Self::new(secret_key.as_bytes())
}
}

impl<C, D> DigestSigner<D, Signature<C>> for Signer<C>
impl<C, D> DigestSigner<D, Signature<C>> for SigningKey<C>
where
C: Curve + Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
Expand All @@ -88,7 +86,7 @@ where
}
}

impl<C> signature::Signer<Signature<C>> for Signer<C>
impl<C> signature::Signer<Signature<C>> for SigningKey<C>
where
C: Curve + Arithmetic + DigestPrimitive,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
Expand All @@ -102,7 +100,7 @@ where

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl<C, D> RandomizedDigestSigner<D, Signature<C>> for Signer<C>
impl<C, D> RandomizedDigestSigner<D, Signature<C>> for SigningKey<C>
where
C: Curve + Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
Expand Down Expand Up @@ -132,7 +130,7 @@ where

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl<C> RandomizedSigner<Signature<C>> for Signer<C>
impl<C> RandomizedSigner<Signature<C>> for SigningKey<C>
where
C: Curve + Arithmetic + DigestPrimitive,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
Expand All @@ -148,7 +146,7 @@ where
}
}

impl<C> From<NonZeroScalar<C>> for Signer<C>
impl<C> From<NonZeroScalar<C>> for SigningKey<C>
where
C: Curve + Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
Expand All @@ -159,7 +157,7 @@ where
}
}

impl<C> Zeroize for Signer<C>
impl<C> Zeroize for SigningKey<C>
where
C: Curve + Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
Expand All @@ -170,7 +168,7 @@ where
}
}

impl<C> Drop for Signer<C>
impl<C> Drop for SigningKey<C>
where
C: Curve + Arithmetic,
C::Scalar: FromDigest<C> + Invert<Output = C::Scalar> + SignPrimitive<C> + Zeroize,
Expand Down
23 changes: 15 additions & 8 deletions ecdsa/src/verifier.rs → ecdsa/src/verify.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! ECDSA verifier. Generic over elliptic curves.
//! ECDSA verify key. Generic over elliptic curves.
//!
//! Requires an [`elliptic_curve::Arithmetic`] impl on the curve, and a
//! [`VerifyPrimitive`] impl on its associated `AffinePoint` type.
Expand All @@ -17,12 +17,12 @@ use elliptic_curve::{
};
use signature::{digest::Digest, DigestVerifier};

/// ECDSA verifier
pub struct Verifier<C: Curve + Arithmetic> {
/// ECDSA verify key
pub struct VerifyKey<C: Curve + Arithmetic> {
public_key: C::AffinePoint,
}

impl<C> Verifier<C>
impl<C> VerifyKey<C>
where
C: Curve + Arithmetic,
C::AffinePoint: VerifyPrimitive<C> + FromEncodedPoint<C>,
Expand All @@ -31,8 +31,15 @@ where
UncompressedPointSize<C>: ArrayLength<u8>,
SignatureSize<C>: ArrayLength<u8>,
{
/// Create a new verifier
pub fn new(public_key: &EncodedPoint<C>) -> Result<Self, Error> {
/// Initialize [`VerifyKey`] from a SEC1-encoded public key.
pub fn new(bytes: &[u8]) -> Result<Self, Error> {
EncodedPoint::from_bytes(bytes)
.map_err(|_| Error::new())
.and_then(|point| Self::from_encoded_point(&point))
}

/// Initialize [`VerifyKey`] from an [`EncodedPoint`].
pub fn from_encoded_point(public_key: &EncodedPoint<C>) -> Result<Self, Error> {
let affine_point = C::AffinePoint::from_encoded_point(public_key);

if affine_point.is_some().into() {
Expand All @@ -45,7 +52,7 @@ where
}
}

impl<C, D> DigestVerifier<D, Signature<C>> for Verifier<C>
impl<C, D> DigestVerifier<D, Signature<C>> for VerifyKey<C>
where
C: Curve + Arithmetic,
D: Digest<OutputSize = C::FieldSize>,
Expand All @@ -59,7 +66,7 @@ where
}
}

impl<C> signature::Verifier<Signature<C>> for Verifier<C>
impl<C> signature::Verifier<Signature<C>> for VerifyKey<C>
where
C: Curve + Arithmetic + DigestPrimitive,
C::AffinePoint: VerifyPrimitive<C>,
Expand Down
6 changes: 3 additions & 3 deletions ed25519/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! [`signature::Verifier`] traits defined in the [`signature`] crate.
//!
//! These traits allow crates which produce and consume Ed25519 signatures
//! to be written abstractly in such a way that different signer/verifier
//! to be written abstractly in such a way that different signing_key/verifier
//! providers can be plugged in, enabling support for using different
//! Ed25519 implementations, including HSMs or Cloud KMS services.
//!
Expand Down Expand Up @@ -37,7 +37,7 @@
//! use ed25519::signature::{Signer, Verifier};
//!
//! pub struct HelloSigner<S> {
//! pub signer: S
//! pub signing_key: S
//! }
//!
//! impl<S> HelloSigner<S>
Expand All @@ -48,7 +48,7 @@
//! // NOTE: use `try_sign` if you'd like to be able to handle
//! // errors from external signing services/devices (e.g. HSM/KMS)
//! // <https://docs.rs/signature/latest/signature/trait.Signer.html#tymethod.try_sign>
//! self.signer.sign(format_message(person).as_bytes())
//! self.signing_key.sign(format_message(person).as_bytes())
//! }
//! }
//!
Expand Down