Commit Graph

10 Commits

Author SHA1 Message Date
Tobin C. Harding fd4586eaae
Invert dependency between io and hashes
Currently in order to release `hashes v1.0` we need to 1.0 `io` as well.
For multiple reasons, many out of our control, the `io` crate may not
stabalise any time soon.

Instead we can invert the dependency between the two crates.

This is an ingenious idea, props to Kixunil for coming up with it.

Notes

- `io` does not currently re-export the `hashes` crate.
- This work highlights that we cannot call `hash_reader` on a siphash.
- The `Hmac::hash_reader` uses the default key which may not be obvious.

Signed-off-by: Tobin C. Harding <me@tobin.cc>
2025-02-11 09:17:21 +11:00
Tobin C. Harding ba6425947f
hashes: Use associated cost for pre-tagging
Instead of requiring users of the `Tag` trait to implement the `engine`
method we can have an associated const and provide an `engine` method
with a default implementation.

Use an associated const for the pre-tagged hash engine. Fro now keep the
`engine` trait method but have a default impl that returns the const. We
will remove it as a separate patch to assist review.
2025-02-08 13:27:30 +11:00
Tobin C. Harding c352d376ed
Do not implement Default for HmacEngine
The `HmacEngine` should be created using a key. Currently we are
providing a `Default` impl that uses `&[]` as the key. This is, I
believe, a hangover from when we had a `Default` trait bound somewhere
else. It is incorrect and an API footgun - remove it.
2025-01-31 09:58:06 +11:00
Tobin C. Harding ec06028f63
hashes: Make hex dependency optional
The only reason we need `hex-conservative` is to parse strings and
format them as hex. For users that do not require this functionality we
can make the `hex-conservative` crate an optional dependency.

The `serde` feature requires `Display` so we enable `hex` from the
`serde` feature.

If `hex` feature is not enabled we still need to be able to debug so
provide `fmt::Debug` functionality by way of macros.

Close: #2654
2024-11-14 09:36:55 +11:00
Tobin C. Harding ae93e226e3
Remove hashes io feature
Currently we only get `std::io::Write` impls when the `bitcoin-io`
dependency is used. This is overly restrictive, it would be nice to have
`std::io::Write` imlps even without the `bitcoin-io` dependency.

Copy the logic out of the `bitcoin_io::impl_write` macro into `hashes`
but feature gate it differently.

Call the new macro inside `hash_type` (and in `hmac`), remove the
`impls` module, and move the tests to the integration test directory.

Remove the `io` feature from `hashes`, now if users enable `std` they
get `std::io::Write` impls and if they enable `bitcoin-io` they get
`bitcoin_io::Write` impls as well.
2024-09-09 06:37:49 +10:00
Nick Johnson 2969b032f9 Push up the Default bound on HashEngine
* The Default bound only makes sense for unkeyed hash functions which
can fire up a new engine without a key. Keyed hash functions, like
SipHash24 or Poly1305 require a secret key to be initialized and
should not implement a default engine generator.
* SipHash24 tests updated to the previous default key "0".
2024-07-31 13:13:51 -07:00
Tobin C. Harding dcb18bfa7a
Add length to sha256::Midstate
In a `HashEngine` the `length` field represents number of bytes
input into the hash engine.

Note also:

> the midstate bytes are only updated when the compression function is
run, which only happens every 64 bytes.

Currently our midstate API allows extracting the midstate after any
amount of input bytes, this is probably not what users want.

Note also that most users should not be using the midstate API anyways.

With all this in mind, add a private `length` field to the `Midstate`
struct and enforce an invariant that it is modulo 64.

Add a single const `Midstate` constructor that panics if the invariant
is violated. The `Midstate` is niche enough that panic is acceptable.

Remove the `from_slice`, `from_byte_array`, and `to_byte_array`
functions because they no longer make sense. Keep `AsRef<[u8]>` for
cheap access to the midstate's inner byte slice.

Note change to `Debug`: `bytes` field now does not include the `0x`
prefix because `as_hex` because of the use of `debug_struct`.

Enjoy nice warm fuzzy feeling from hacking on crypto code.
2024-07-18 06:16:45 +10:00
Andrew Poelstra 73dcc79763
hashes: split Hash trait into two 2024-06-24 13:28:54 +00:00
Fmt Bot 4499c4c046 2024-05-26 automated rustfmt nightly 2024-05-26 01:07:01 +00:00
Tobin C. Harding 4446be6fc8
hashes: Add regression tests
We have regression tests spread out throughout the `hashes` module but
they are not labelled as such. To give us more confidence and help
debug when patching the `hashes` crate we can add a bunch of regression
tests in a single place.

Add a module that does a single regression test for each type, simply
hash some arbitrary data and check the hex display against a hard coded
hex string.
2024-05-22 10:22:05 +10:00