tc_md
The MD2 (RFC 1319), MD4 (RFC 1320) and MD5 (RFC 1321) message digests, ported
from Bouncy Castle, for interoperability with legacy data and protocols. Every
digest implements the tc_digest
traits.
All three algorithms are cryptographically broken. Collisions are cheap for
each of them, chosen-prefix collisions included for MD5, and MD2 and MD4 also
fall to preimage attacks. Never use them for signatures, integrity against an
attacker, password hashing or any new design; use a SHA-2 digest from
tc_sha instead.
The crate is no_std, needs no allocator and contains no unsafe code. It
depends on tc_digest and on tc_zeroize,
which wipes the digest state.
Requires Rust 1.85 or later (edition 2024).
Types
Md2Digest— MD2, 16-byte digest, 16-byte blocks; variable time.Md4Digest— MD4, 16-byte digest, 64-byte blocks; constant time.Md5Digest— MD5, 16-byte digest, 64-byte blocks; constant time.
Each digest implements TryDigest with core::convert::Infallible as its
error type, and so Digest as well. It has const fn new and Default, and
implements Clone, so a digest can be forked partway through a message.
Display writes the algorithm name, MD2, MD4 or MD5.
do_final writes the 16-byte digest to the start of the output buffer, leaves
any longer tail untouched, returns 16, and resets the digest. It panics if the
buffer is shorter than 16 bytes, before changing any state.
Timing
MD4 and MD5 use only additions, rotations and bitwise operations, so their
running time depends only on the message length. MD2 indexes a 256-byte S-box
with bytes derived from the message, which can leak the message through cache
timing: hash only public data with Md2Digest.
Wiping
Dropping a digest, or resetting it, which do_final also does, wipes its
state with volatile writes through tc_zeroize: the chaining registers, the
buffered block and its fill level, and the length count, or for MD2 the state,
the buffered block and the checksum. A clone is wiped when it is dropped in
turn. Copies the compression leaves in registers or on the stack, such as the
message words it reads, and copies a move leaves behind are not wiped, and
drop-based wiping requires the destructor to run. The output buffer belongs to
the caller.
Usage
[]
= "0.1.0"
= "0.1.1"
Import Digest to reach the digest's methods:
use Digest;
use Md5Digest;
let mut md5 = new;
md5.update;
md5.update;
let mut output = ;
assert_eq!;
assert_eq!;
Validation
The tests check each digest against the complete test suites of RFC 1319,
RFC 1320 and RFC 1321, and MD4 and MD5 against digests computed by OpenSSL for
messages on the 64-byte padding boundaries and for one million 'a' bytes.
Contract tests check, for every digest, that splitting a message across
updates never changes the digest, that do_final writes exactly 16 bytes and
resets, that a short output buffer panics without changing state, and that a
clone continues independently. A unit test checks that the block buffer's
wipe clears its buffered bytes, fill level and length count, and a contract
test checks that every digest runs a destructor. Another test requires every
digest type and internal helper to document whether it is constant or variable
time. Missing
public documentation is rejected by a crate-level lint, and unsafe code is
forbidden.
Run these commands from the workspace root:
cargo test -p tc_md --locked
cargo clippy -p tc_md --all-targets --locked -- -D warnings
cargo fmt -p tc_md --check
cargo doc -p tc_md --no-deps --locked
Before a release, check the archive contents and run publication validation from a committed checkout:
cargo package -p tc_md --list --locked
cargo publish -p tc_md --dry-run --locked
The archive includes both license texts, this README, the changelog, the
source and the tests. It must not include target/ or other build artifacts.
License
Licensed under either the MIT license or the Apache License, Version 2.0, at your option.