Skip to content

Commit

Permalink
ecdsa: add hazmat primitives; remove/reverse curve deps (#96)
Browse files Browse the repository at this point in the history
Adds "hazmat" ECDSA signing and verification traits intended to be
implemented by individual elliptic curve implementations:

- `SignPrimitive`: intended to be implemented on `Scalar`
- `VerifyPrimitive`: intended to be implemented on `AffinePoint`

The traits are generic over elliptic curves, allowing one type to
potentially support multiple curves. This is potentially useful for
things like FFI bindings to multi-curve libraries, or host libraries for
hardware devices which support ECDSA signing for multiple elliptic
curves.

These traits must be consumed directly by elliptic curve
implementations, which means we need to reverse the current relationship
where the `ecdsa` crate has optional features for `k256`, `p256`, and
`p384`.

Instead, we can add an `ecdsa` feature to the `k256`, `p256`, and `p384`
crates which optionally pulls this crate in.

With the dependency relationship reversed, we can support an open ended
number of elliptic curves including 3rd party non-RustCrypto
implementations (as well as 3rd party ECDSA implementations ala
afforementioned hardware tokens).

This allows the `ecdsa` crate to focus on only the high-level details of
the ECDSA algorithm, like RFC 6979 deterministic signatures.

It also allows for wrapping complete ECDSA implementations, including
assembly optimized ECDSA primitives or things like hardware
accelerators.
  • Loading branch information
tarcieri authored Jul 13, 2020
1 parent 9d8f4b6 commit 30390b3
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 677 deletions.
36 changes: 1 addition & 35 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
members = ["ecdsa", "ed25519"]

[patch.crates-io]
elliptic-curve = { git = "https://github.com/RustCrypto/elliptic-curves" }
k256 = { git = "https://github.com/RustCrypto/elliptic-curves" }
p256 = { git = "https://github.com/RustCrypto/elliptic-curves" }
p384 = { git = "https://github.com/RustCrypto/elliptic-curves" }
elliptic-curve = { git = "https://github.com/RustCrypto/traits" }
23 changes: 2 additions & 21 deletions ecdsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@ version = "0.4"
default-features = false
features = ["weierstrass"]

[dependencies.k256]
version = "0.3"
optional = true
default-features = false

[dependencies.p256]
version = "0.3"
optional = true
default-features = false

[dependencies.p384]
version = "0.2"
optional = true
default-features = false

[dependencies.sha2]
version = "0.9"
optional = true
Expand All @@ -44,14 +29,10 @@ version = ">= 1.1.0, < 1.2.0"
default-features = false

[features]
default = ["digest", "std", "zeroize"]
default = ["digest", "std"]
digest = ["signature/digest-preview", "sha2"]
secp256k1 = ["k256"]
secp256k1-arithmetic = ["k256/arithmetic", "secp256k1"]
hazmat = []
std = ["elliptic-curve/std", "signature/std"]
zeroize = ["elliptic-curve/zeroize"]
test-vectors = []

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
2 changes: 1 addition & 1 deletion ecdsa/src/asn1_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use crate::{
convert::ScalarPair,
curve::Curve,
generic_array::{typenum::Unsigned, ArrayLength, GenericArray},
Error,
};
Expand All @@ -11,6 +10,7 @@ use core::{
fmt::{self, Debug},
ops::Add,
};
use elliptic_curve::weierstrass::Curve;

/// Maximum overhead of an ASN.1 DER-encoded ECDSA signature for a given curve:
/// 9-bytes.
Expand Down
35 changes: 24 additions & 11 deletions ecdsa/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,35 @@ where

#[cfg(all(test, feature = "test-vectors"))]
mod tests {
use crate::{
curve::nistp256::{Asn1Signature, FixedSignature},
test_vectors::nistp256::SHA256_FIXED_SIZE_TEST_VECTORS,
};
use elliptic_curve::{consts::U32, weierstrass::Curve};
use signature::Signature;

#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct ExampleCurve;

impl Curve for ExampleCurve {
type ScalarSize = U32;
}

type Asn1Signature = crate::Asn1Signature<ExampleCurve>;
type FixedSignature = crate::FixedSignature<ExampleCurve>;

const EXAMPLE_SIGNATURE: [u8; 64] = [
0xf3, 0xac, 0x80, 0x61, 0xb5, 0x14, 0x79, 0x5b, 0x88, 0x43, 0xe3, 0xd6, 0x62, 0x95, 0x27,
0xed, 0x2a, 0xfd, 0x6b, 0x1f, 0x6a, 0x55, 0x5a, 0x7a, 0xca, 0xbb, 0x5e, 0x6f, 0x79, 0xc8,
0xc2, 0xac, 0x8b, 0xf7, 0x78, 0x19, 0xca, 0x5, 0xa6, 0xb2, 0x78, 0x6c, 0x76, 0x26, 0x2b,
0xf7, 0x37, 0x1c, 0xef, 0x97, 0xb2, 0x18, 0xe9, 0x6f, 0x17, 0x5a, 0x3c, 0xcd, 0xda, 0x2a,
0xcc, 0x5, 0x89, 0x3,
];

#[test]
fn test_fixed_to_asn1_signature_roundtrip() {
for vector in SHA256_FIXED_SIZE_TEST_VECTORS {
let fixed_signature = FixedSignature::from_bytes(&vector.sig).unwrap();
let fixed_signature = FixedSignature::from_bytes(&EXAMPLE_SIGNATURE).unwrap();

// Convert to DER and back
let asn1_signature = Asn1Signature::from(&fixed_signature);
let fixed_signature2 = FixedSignature::from(&asn1_signature);
// Convert to DER and back
let asn1_signature = Asn1Signature::from(&fixed_signature);
let fixed_signature2 = FixedSignature::from(&asn1_signature);

assert_eq!(fixed_signature, fixed_signature2);
}
assert_eq!(fixed_signature, fixed_signature2);
}
}
33 changes: 0 additions & 33 deletions ecdsa/src/curve.rs

This file was deleted.

19 changes: 0 additions & 19 deletions ecdsa/src/curve/nistp256.rs

This file was deleted.

19 changes: 0 additions & 19 deletions ecdsa/src/curve/nistp384.rs

This file was deleted.

54 changes: 0 additions & 54 deletions ecdsa/src/curve/secp256k1.rs

This file was deleted.

Loading

0 comments on commit 30390b3

Please sign in to comment.