Hash, MAC and KDF building blocks
This no_std crate provides SHA-256/SHA-512 whole-message and raw compression
APIs, direct Rust streaming state, prepared HMAC-SHA256/SHA512, HKDF-SHA256,
BLAKE2s-256 hash/keyed MAC, Ethereum Keccak-256 and the pinned BLAKE3 implementation. Algorithms
borrow inputs; outputs are fixed arrays or caller-provided slices. None needs
an allocator. baseline exposes independently maintained RustCrypto controls.
keccak256, keccak256_parts and Keccak256::{new,update,digest,finalize}
use the pinned RustCrypto sha3 = 0.10.9 Keccak-256 implementation. The parts
and streaming APIs borrow input without a concatenation allocation. digest
clones the public hashing state without consuming it. This is Ethereum's
Keccak padding, not FIPS SHA3-256. Never substitute SHA3-256 or BLAKE3 when
reconstructing an Ethereum address or EIP-712 authorization digest. Hashing
does not add application replay protection or authenticate a public key.
sha256_simd4::{sha2564, Sha256x4} groups four independent standard SHA-256
messages. sha256_sms4::{sha256_sms4, Sha256Sms4} schedules consecutive blocks
of a long message while preserving serial chaining. The default short-stream
kernel stays scalar. See the API guide for choosing a path
and the qualified measurements
for complete instruction/page costs, standard vectors and emitted wipe checks.
Sha256::update and Sha512::update borrow full blocks directly and retain one
partial block. digest does not consume a stream; finalize does. HMAC keys
prepare inner/outer chaining states once; .stream() creates an independent
direct context and .tag() performs a complete MAC. .verify() compares all
tag bytes in constant time. Owned SHA/HMAC state erases on drop, without a
promise about every compiler temporary or caller copy.
The outer HMAC digest is compressed directly as words with fixed padding;
finalization does not clone a stream or serialize and decode the inner digest.
HMAC-SHA256 uses direct word schedules for 32-byte keys and messages. HKDF with
32-byte IKM carries Extract's PRK words directly into Expand's key preparation.
A single output block with at most 54 info bytes also uses direct final-block
compression. Longer inputs and outputs retain the general streaming path.
These paths use the same SHA rounds and erase their owned word buffers; only
public lengths determine dispatch and indexing.
HkdfSha256::extract returns the 32-byte PRK. extract_parts accepts borrowed
IKM fragments. new performs Extract and prepares Expand; from_prk prepares
a supplied 32-byte PRK. expand and expand_parts write at most 8160 bytes,
reject larger outputs before writing, and accept empty output. Callers own any
PRK bytes returned by Extract.
from_prepared_salt reuses caller-owned HMAC prefixes for independent IKM under
the same salt. Omitted salt uses compile-time compression results for the
specified zero salt, with no runtime cache or caller-key content test.
BLAKE2s keys are 0..=32 bytes; an empty key selects ordinary BLAKE2s. A
Blake2sKey retains the upstream context for reuse. Its pinned implementation
does not promise complete secret erasure; do not infer that property from the
SHA/HMAC implementations.
BLAKE3 defaults to the existing wasm64 SIMD patch when built on Wasm with the
wasm-simd feature. The canonical SDK build enables simd128; native builds
retain upstream native selection. blake3::Hasher::{new_portable, new_keyed_portable,new_derive_key_portable} explicitly pin the portable backend
inside the same artifact, including resets and tree-parent compression. They
do not rely on Cargo feature isolation. Upstream's zeroize feature supplies
explicit erasure support, not an automatic guarantee for every caller context.
Source and licenses
SHA-256's unrolled 16-word rolling schedule and fixed-length paths come from
the Motoko plugin. SHA-512's borrowed batching follows its current runtime;
its scalar 80-round kernel applies the same constant-index rolling structure
to FIPS 180-4.
Their notices and Apache-2.0 WITH LLVM-exception are preserved in
MOTOKO-NOTICE, MOTOKO-LICENSE and provenance/motoko.json. The SHA/HMAC
Motoko Blob carriers and canonical serialization are omitted: Rust state is
owned directly. Independent SHA controls retain sha2 = 0.10.9; BLAKE2s uses
blake2 = 0.10.6. HMAC/HKDF controls use 0.12.1/0.12.4. Exact registry source
identities, upstream commits and licenses are under provenance/.
Keccak reuses the existing pinned sha3 registry package, without copying or
modifying its permutation. Its MIT/Apache-2.0 source identity is recorded in
provenance/ethereum-keccak.json; known answers and streaming boundary tests
live beside the wrapper. This addition has no measured IC cost claim.
vendor/blake3 preserves BLAKE3 1.8.7 and its license files. Its three existing
wasm64 gate/import changes are recorded in vendor/UPSTREAM.showtime.json.
vendor/UPSTREAM.json records the added portable constructors and current
modified-file hashes. The compression function, flags, tree and byte contracts
are unchanged. The vendor includes upstream native backends; pure avoids C
assembly while retaining the existing Rust/native and Wasm SIMD choices.
The opt-in independent-query PRG scheduling is recorded separately in
vendor/PRG-QUERY-BATCH-PROVENANCE.json.
It reuses the pinned seven-round SIMD equations for four nine-byte query
suffixes, preserves the derive-key/XOF transcript and does not edit src/lib.rs.
The measured public-length dispatch keeps single-query readers for one through six
outputs and selects the existing query SIMD kernel from seven onward; query
admission and empty handling still precede every output write.
Validation and measurements
Unit tests cover FIPS/RFC known answers, independent RustCrypto comparisons,
stream partitions and continuation, complete raw compression, key-length
boundaries, HKDF maximum output and non-writing rejection. Checked-in official
BLAKE3 vectors cover all modes and seekable output on portable and selected
backends; official BLAKE2s vectors cover hash/keyed padding boundaries. Vector
origins and hashes are retained under provenance/ and vendor/.
Benchmark adapters live in the hash fixture; setup and expected
results precede timing. Portable/selected BLAKE3 cases use identical one-update
Hasher call shapes. Whole hashing and raw compression are different workloads.
Prepared fixture variants hold their native states in separate boxes; a raw
hash fixture reserves only pointer/tag metadata for absent prepared state.
This fixture-layout correction is not an algorithm instruction win.
The bulk-key, bulk-prk and full-bulk-salt cases instead construct their
shared context inside each measured call. Their matching fresh cases repeat
setup for every item, using the same keys and messages. Distinct inputs in the
shared-key/PRK pairs vary messages while keeping the shared key fixed. Each
item still computes and checks its complete independent output. Prefix reuse
follows RFC 2104, and
HKDF retains the RFC 5869
Extract/Expand construction.
The fixed-width user-key fixtures use IKM32, salt32, the 15-byte domain
rsx.user-key.v1 followed by ID32, and output32. The primary distinct-secret
case performs full Extract and Expand for each user. Its separately named
distinct-salt-reused variant prepares the common public salt once inside
each call, while still extracting every distinct secret independently. The
shared-master variants are a separate workload. All expected user keys come
from the independent RustCrypto HKDF control.
New source locations require fresh artifact qualification; old Motoko/Showtime
instruction receipts do not qualify these crates.