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
1 change: 1 addition & 0 deletions .github/workflows/ecdsa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
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 }}

test:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions ecdsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ hazmat = []
rand = ["elliptic-curve/rand_core", "signature/rand-preview"]
signer = ["digest", "hazmat", "rand", "zeroize"] # TODO(tarcieri): deterministic signing
std = ["elliptic-curve/std", "signature/std"]
verifier = ["digest", "hazmat"]
zeroize = ["elliptic-curve/zeroize"]

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

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

// Re-export the `elliptic-curve` crate (and select types)
pub use elliptic_curve::{
self, generic_array,
Expand All @@ -50,6 +54,9 @@ pub use signature::{self, Error};
#[cfg(feature = "signer")]
pub use signer::Signer;

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

use core::{
convert::TryFrom,
fmt::{self, Debug},
Expand Down
71 changes: 71 additions & 0 deletions ecdsa/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! ECDSA verifier. Generic over elliptic curves.
//!
//! Requires an [`elliptic_curve::Arithmetic`] impl on the curve, and a
//! [`VerifyPrimitive`] impl on its associated `AffinePoint` type.

use crate::{
hazmat::{DigestPrimitive, VerifyPrimitive},
Error, Signature, SignatureSize,
};
use core::ops::Add;
use elliptic_curve::{
consts::U1,
generic_array::ArrayLength,
weierstrass::{CompressedPointSize, Curve, FromPublicKey, PublicKey, UncompressedPointSize},
Arithmetic,
};
use signature::{digest::Digest, DigestVerifier};

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

impl<C> Verifier<C>
where
C: Curve + Arithmetic,
C::AffinePoint: VerifyPrimitive<C> + FromPublicKey<C>,
C::ElementSize: Add<U1>,
<C::ElementSize as Add>::Output: Add<U1>,
CompressedPointSize<C>: ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
SignatureSize<C>: ArrayLength<u8>,
{
/// Create a new verifier
pub fn new(public_key: &PublicKey<C>) -> Result<Self, Error> {
let affine_point = C::AffinePoint::from_public_key(public_key);

if affine_point.is_some().into() {
Ok(Self {
public_key: affine_point.unwrap(),
})
} else {
Err(Error::new())
}
}
}

impl<C, D> DigestVerifier<D, Signature<C>> for Verifier<C>
where
C: Curve + Arithmetic,
D: Digest<OutputSize = C::ElementSize>,
C::AffinePoint: VerifyPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
fn verify_digest(&self, digest: D, signature: &Signature<C>) -> Result<(), Error> {
self.public_key
.verify_prehashed(&digest.finalize(), signature)
}
}

impl<C> signature::Verifier<Signature<C>> for Verifier<C>
where
C: Curve + Arithmetic + DigestPrimitive,
C::AffinePoint: VerifyPrimitive<C>,
C::Digest: Digest<OutputSize = C::ElementSize>,
SignatureSize<C>: ArrayLength<u8>,
{
fn verify(&self, msg: &[u8], signature: &Signature<C>) -> Result<(), Error> {
self.verify_digest(C::Digest::new().chain(msg), signature)
}
}