Commit Graph

327 Commits

Author SHA1 Message Date
Tony Arcieri ff9a2058bd ecdsa: use NonZeroScalar arguments to sign_prehashed (#794)
Provides type-level enforcement that these parameters are non-zero
2024-01-17 19:07:59 -07:00
Tony Arcieri 3ed9867409 ecdsa: remove SignPrimitive and VerifyPrimitive traits (#793)
The backstory of these traits was once upon a time we didn't yet have
the trait structure in place to express algorithms like ECDSA signing
and verification generically, so each crate (at the time just `k256` and
`p256`) had a nearly duplicated implementation of ECDSA, with `k256`
including tweaks for low-S normalization.

Now the `ecdsa` crate contains fully generic implementations of both
algorithms, and with the `EcdsaCurve` trait, carries a `NORMALIZE_S`
preference, so these traits are just needless indirection at this point.

This removes the traits, converting non-trivial methods into static
functions in the `hazmat` module, namely `sign_prehashed_rfc6979`.
2024-01-17 18:18:59 -07:00
Tony Arcieri c5206d43db ecdsa: use EcdsaCurve in bounds (#792)
Replaces previous `PrimeCurve` bounds with the new `EcdsaCurve` trait
(which has a supertrait bound on `PrimeCurve`).
2024-01-17 16:14:01 -07:00
Tony Arcieri 56b8b7acdf ecdsa: move EcdsaCurve trait to toplevel (#791)
Moves the trait added in #787 out of the `hazmat` module and into the
toplevel, so it's available regardless of whether or not the `hazmat`
feature has been enabled.
2024-01-17 14:20:52 -07:00
Tony Arcieri 3992dc30e0 ecdsa: don't feature-gate EcdsaCurve (#790)
There's no reason for it to be gated on `arithmetic`. It can be provided
in all use cases.
2024-01-17 13:09:03 -07:00
Tony Arcieri 9eda95a623 ecdsa: add EcdsaCurve marker trait (#787)
This trait is the intended successor to the `SignPrimitive` and
`VerifyPrimitive` traits.

Currently the only reason for a non-default impl of those traits is to
handle low-S normalization. The `EcdsaCurve` trait now not only marks
the curve as being safe for use with ECDSA, but also captures this
decision regarding low-S normalization so it doesn't have to be
expressed in code.
2024-01-17 12:16:17 -07:00
Tony Arcieri 81adf2e2fc ecdsa: add DER support to SignatureWithOid (#786)
Adds the following methods:

- `from_der_with_digest`
- `from_der_with_oid`
- `to_der`

These handle deserializing/serializing ASN.1 DER-encoded signatures.
2024-01-17 10:17:26 -07:00
Tony Arcieri 4a94edb56a Bump digest, elliptic-curve, and signature; MSRV 1.72 (#785)
Bumps the following dependencies:
- `digest` v0.11.0-pre.7
- `elliptic-curve` v0.14.0-pre.3
- `signature` v2.3.0-pre.2
2024-01-17 10:07:35 -07:00
Tony Arcieri 970aacfaea ecdsa: gate pkcs8::Encode*Key impls under alloc+pkcs8 (#783)
...rather than `pem`, as these can be used to encode DER without having
full `pem` support enabled.

Closes #721
2024-01-17 08:09:20 -07:00
Tony Arcieri 32edd0d630 ecdsa: make Signature::normalize_s infallible (#780)
Unconditionally returns a normalized signature, regardless of whether
the signature was normalized to begin with.

Closes #736
2024-01-16 18:33:49 -07:00
Tony Arcieri 83359e104d ecdsa: reduce z mod q when performing RFC6979 (#777)
The RFC specifies this, however we were not performing it.

Going forward, it would be nice to be able to refactor these APIs to
operate over a `Scalar` which we know is always reduced.
2024-01-16 17:57:05 -07:00
Tony Arcieri 8e12e1c7cd ecdsa: remove OutputSize bounds on DigestPrimitive (#774)
Notably for curves like P-521, the digest used to compute the signature
is smaller than a serialized field element (SHA-512 w\ 64-byte output vs
66-byte serialized field elements).

To support such curves, we need to remove this bound.

The already implemented `bits2field` function as defined in
RFC6979 § 2.3.2 and SEC1 § 2.3.8 handles producing a serialized field
element from an input which may be a different size.
2024-01-16 14:06:50 -07:00
Tony Arcieri 2472e1cb7f rfc6979: add generate_k_mut; remove digest bounds (#773)
Adds an API which writes `k` into an output buffer rather than
allocating and returning it, which also accepts slices as inputs. This
makes it possible to use `rfc6979` to implement the `dsa` crate.

Also removes output size bounds on the underlying digest function, which
aren't actually relevant to the implementation at all since HMAC-DRBG
writes a variable-sized amount of output. This makes it possible to use
`rfc6979` + `ecdsa` in conjunction with `p521`, which has unusually
sized scalars (66-bytes) which don't match the output size of the
underlying digest function (SHA-512, which has a 64-byte output).
2024-01-16 09:57:05 -07:00
Bernd Krietenstein b41c216560 Fixed rfc reference in comment. (#772) 2024-01-12 05:55:44 -07:00
Tony Arcieri 2cb7693956 ecdsa: fix new_wycheproof_test! macro (#770)
Updates `from_exact_iter` => `from_iter`
2024-01-10 14:11:13 -07:00
Tony Arcieri 1e5e29cc50 Bump elliptic-curve and signature dependencies (#769)
As well as bumping `hybrid-array` to v0.2.0-rc.0 and `digest` to
v0.11.0-pre.4.

Also cuts `ecdsa` v0.17.0-pre.1 and `rfc6979` v0.5.0-pre.1
2024-01-10 13:34:47 -07:00
Tony Arcieri d935382d8d ecdsa: fix dev macros (#768)
The method changed from `GenericArray::from_slice` to
`Array::ref_from_slice`.

This has been renamed back upstream in `hybrid-array` to simplify
people's upgrades, but for now we need to rename it until we can upgrade
`hybrid-array` again.

Longer term the method will be deprecated, so this is temporary anyway.
2024-01-10 08:16:08 -07:00
Tony Arcieri 4ee30f578e Bump signature to v2.3.0-pre.0; MSRV 1.71 (#765)
For `ecdsa` crate: MSRV is 1.73 due to a bump of `elliptic-curve` to
v0.14.0-pre.0.

This commit begins the next round of breaking changes and bumps all
crates to prerelease versions.

The main change in `signature` is an upgrade of the `digest` crate to
v0.11.0-pre.3, which is what brings the new MSRV 1.71 requirement.
2024-01-09 12:07:49 -07:00
Tony Arcieri e8d4d23a43 ed25519: hexify Debug impl on Signature (#747)
Formats the `R` and `s` signature components as hexidecimal to make the
representation more compact, as requested in #723:

ed25519::Signature {
    R: 0x3f3e3d3c3b3a393837363534333231302f2e2d2c2b2a29282726252423222120,
    s: 0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100,
}
2023-10-15 12:47:01 -06:00
Tony Arcieri d7557a62bc ecdsa: extract hazmat::{sign_prehashed, verify_prehashed}. (#731)
Extracts generic, reusable functions from the `SignPrimitive` and
`VerifyPrimitive` traits.

The main motivation for this is to make it possible for the `*Primitive`
trait impls to be composed in terms of a generic implementation, in
order to add support for low-S normalization in the `k256` crate.
2023-07-17 16:55:05 -06:00
Tony Arcieri 2c3d90ed6f ecdsa: refactor Signature constructors and improve docs (#730)
Implements `from_bytes` in terms of `from_scalars`, rather than the
other way around.

This places the logic for checking that `r` and `s` are nonzero inside
of the `from_scalars` method.

Additionally improves the documentation to note the various failure
cases, i.e. if `r` and/or `s` is out of the range `1..n`, where `n` is
the scalar modulus.
2023-07-10 12:46:56 -06:00
Arthur Gautier 7bf1313e85 ecdsa: Adds support for the SignatureBitStringEncoding trait (#716) 2023-05-04 16:29:24 -06:00
Tony Arcieri 800eda2a07 ecdsa: make fmt impls more consistent (#713) 2023-04-14 11:30:03 -06:00
Tony Arcieri 31ebc99a15 ecdsa: serde doc fixup (#712)
The width of a serialized `Signature` is generic and based on the size
of the underlying curve's modulus, not a fixed 64-bytes.
2023-04-14 10:57:19 -06:00
Tony Arcieri 9433d578fa ecdsa: change der::Signature citation to RFC5912 (#711) 2023-04-13 14:21:53 -06:00
Tony Arcieri b776db27b0 ecdsa: add RFC5480 citation for der::Signature (#710)
Adds a reference to the ASN.1 schema and where it's defined
2023-04-13 14:09:26 -06:00
Tony Arcieri c3e35b13f2 ecdsa: fix test macros handling of serialized field size (#707)
Uses `C::FieldBytesSize` instead of `C::Uint::BYTES`, which is needed
for curves like P-224 where the serialized field element may be smaller
than the bigint used to represent them.
2023-04-09 18:13:59 -06:00
Tony Arcieri d18b8a90f3 ecdsa: use C::FieldBytesSize over C::Uint::BYTES (#705)
This is needed to make curves like P-224 and P-521 work, since the size
of the `C::Uint` may not match the size of an integer serialization of a
field element (since `C::Uint::MAX` may be much larger than the field
modulus)
2023-04-08 20:27:34 -06:00
Tony Arcieri 621a847b8a ecdsa: fix RecoveryId computation in SignPrimitive (#702)
The implementation was adapted from `k256`, which produces signatures
with low-S normalization.

However, the provided implementation does not low-S normalize
signatures, so it should not be considered in the `RecoveryId`
computation.
2023-04-05 12:19:39 -06:00
Tony Arcieri d25b07c18e ecdsa: forward AssociatedAlgorithmIdentifier impls (#698)
...from the `elliptic-curve` crate, as released in v0.13.3
2023-04-04 20:39:20 -06:00
Dmitry Baryshkov 9de47b676e ecdsa: follow the DynAssociatedAlgorithmIdentifier changes (#695)
Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2023-04-04 18:08:25 -06:00
Tony Arcieri bb601997d8 ecdsa: add SignatureWithOid verification support (#690)
Gated under a newly added `sha2` feature.

This implementation dispatches based on RFC5758 OIDs, selecting the
appropriate hash function to use at runtime based on the OID.
2023-04-02 19:59:53 -06:00
Tony Arcieri e05b64f4d8 ecdsa: add SignatureWithOid (#689)
Adds a signature type which is paramaterized by an RFC5758-defined OID
identifying the variant of ECDSA used to produce the signature.
2023-04-02 19:22:20 -06:00
Tony Arcieri 5ebcfef950 ecdsa: impl SignatureAlgorithmIdentifier (#688)
Propagates the `AssociatedAlgorithmIdentifier` from `Signature<C>` to
`SigningKey<C>` and `VerifyingKey<C>`.
2023-04-02 18:31:20 -06:00
Tony Arcieri 56dd91a963 ecdsa: add RFC5758 OID support (#686)
Adds support for obtaining the RFC5758 OIDs associated with the digest
algorithm used with the `Signer` and `Verifier` traits, i.e. the digest
defined via `DigestPrimitive::Digest` for a given curve.

The ECDSA OID is available via an `AssociatedOid` impl on `Signature`.

The `AlgorithmIdentifier` is also available via the newly added
`AssociatedAlgorithmIdentifier` trait.
2023-04-02 16:13:16 -06:00
Tony Arcieri 6121f15d59 ecdsa: add Signature::{from_bytes, from_slice} (#684)
Adds inherent methods for parsing a signature from `SignatureBytes<C>`
and a byte slice respectively.

The existing `TryFrom<&[u8]>` impl now calls `Signature::from_slice`.

These methods match similar inherent methods on other types.
2023-03-28 10:58:49 -06:00
Jonas Lindstrøm e572a42236 ecdsa: handle the reduced R.x case in public key recovery (#680) 2023-03-18 14:11:22 -06:00
Tony Arcieri 7467f2cc4a ecdsa: add VerifyingKey::to_sec1_bytes + more conversions (#675)
Adds an inherent method, gated on `alloc`, for serializing a
`VerifyingKey` as a boxed byte slice containing the key's SEC1 encoding.

Also adds `From` conversions for converting to
`sec1::{CompressedPoint, EncodedPoint}`.
2023-03-08 11:19:05 -07:00
Tony Arcieri 687fb97891 ecdsa: documentation improvements (#668) 2023-03-01 18:47:56 -07:00
Tony Arcieri be9621e146 ecdsa: add Decode and Encode impls for der::Signature (#666) 2023-03-01 09:26:25 -07:00
Tony Arcieri 3d0daf374b ecdsa: bump elliptic-curve to v0.13.0-rc.0; MSRV 1.65 (#660)
Also bumps `der` to v0.7
2023-02-28 13:06:31 -07:00
Tony Arcieri 82776dc4cc ecdsa: add Invert bound (#653)
Previously this was handled by a blanket impl for `ff::Field`
2023-02-03 08:56:10 -07:00
Tony Arcieri 6028260f29 ecdsa: use Scalar::invert_vartime for verification (#651)
Variable-time inversions can be faster than constant-time inversions and
are fine in the context of verification.
2023-02-03 07:25:18 -07:00
Tony Arcieri 4e2b0b2cfc ecdsa: use AffineCoordinates trait (#650)
Switches to the newly consolidated `AffineCoordinates` trait.

See RustCrypto/traits#1237
2023-02-01 09:57:42 -07:00
Tony Arcieri b20161fe04 ecdsa: use FieldBytesEncoding to serialize C::ORDER (#647)
Bumps `elliptic-curve` to v0.13.0-pre.3, and uses the new
`FieldBytesEncoding` API.

This is needed to pass the curve's order to the RFC6979 implementation.
2023-01-31 20:30:34 -07:00
Tony Arcieri 4af28dd9fa Revert "ecdsa: use FromUintUnchecked for serializing order (#644)" (#646)
This reverts commit 298129cbce.

Abusing `from_uint_unchecked` for this broke `p384`, which appears to be
serializing the order as zero. Curiously, this didn't impact `p256`.
2023-01-31 14:29:36 -07:00
Tony Arcieri 298129cbce ecdsa: use FromUintUnchecked for serializing order (#644)
This eliminates a usage of `C::encode_field_bytes`.
2023-01-31 08:05:34 -07:00
Tony Arcieri 1dc9dd45e1 ecdsa: use Reduce::reduce_bytes (#643)
Uses the newly (re)introduced method for reducing an input with
serialized bytes to perform modular reductions on `FieldBytes`.

See: https://github.com/RustCrypto/traits/pull/1229
2023-01-30 21:21:19 -07:00
Tony Arcieri 8d61fce08b ecdsa: use &FieldBytes<C> consistently; fix tests (#642)
Always borrow the input field bytes.

Fix test macros in the `dev` module.
2023-01-30 10:56:20 -07:00
Tony Arcieri 17663bd4f5 ecdsa: bump elliptic-curve to v0.13.0-pre.1 (#640)
Bumps the `elliptic-curve` crate to the latest prerelease on crates.io.

Notably this includes changes to how `FieldBytes` is encoded made with
the goal of supporting elliptic curves with unusual moduli such as P-224
and P-521:

https://github.com/RustCrypto/traits/pull/1220
2023-01-28 19:48:06 -07:00