- OCaml 68%
- C 32%
ocaml-hash #
Warning. This is a work-in-progress experimental fork of digestif. Do not use it in production; use digestif instead.
Cryptographic hash functions for OCaml: MD5, SHA-1, the SHA-2 family (SHA-224/256/384/512 and SHA-512/256) and BLAKE2b. Each has a one-shot and an incremental interface, HMAC and a tap for Bytesrw streams; BLAKE2b also has its own keyed MAC.
Content-addressed storage, package verification and TLS all hash on their hot path. On ARM, the portable C code most OCaml libraries ship never touches the SHA instructions the CPU provides. This package adds an experimental C path for SHA-1, SHA-256 and SHA-512 that uses the ARMv8 cryptographic extensions when the CPU has them, chosen at run time. SHA-224, SHA-384 and SHA-512/256 share those kernels and get the same path. It passes the test vectors and agrees with the portable backends, but nobody has audited the vendored assembly independently in this fork.
On a quiet Apple M-series machine, the one-shot digest_string of a 256 MiB
buffer runs at about 3.16 GB/s for SHA-1, 3.13 GB/s for SHA-256 and 1.78 GB/s
for SHA-512. That is on a par with OpenSSL's hardware paths on the same chip.
The portable C manages about 1.1, 0.43 and 0.66 GB/s. Each figure is the
median of five timed runs after one warm-up, and the run did not record the
exact chip or the date. dune exec bench/bench_hash.exe measures the
accelerated column and HASH_NO_SIMD=1 dune exec bench/bench_hash.exe the
portable one; the OpenSSL comparison has no committed command. In general,
setting the HASH_NO_SIMD environment variable forces the portable paths.
MD5 has no hardware extension and runs on a par with OpenSSL.
These numbers move with machine load. A machine busy compiling can halve them, so benchmark on a quiet one and compare ratios.
The package is a fork of digestif by Romain Calascibetta (MIT; the bundled C carries Vincent Hanquez's BSD-2-Clause notice, see LICENSE.md). It keeps the algorithms listed above. Its C symbols are renamed so that both libraries can be linked into one binary, and it adds the hardware C path and the Bytesrw API.
hash is a dune virtual library. It declares only an interface, and each
executable picks an implementation at link time, so code that depends on
hash compiles once against the interface. Two implementations exist.
hash.c, in lib/c/, links the C code described above and is the default.
hash.ocaml, in lib/ocaml/, is pure OCaml with no C stubs, so it builds and
runs under js_of_ocaml; a js_of_ocaml target selects it by listing
hash.ocaml in its libraries. Both give byte-identical digests.
Installation #
Install with opam:
$ opam install hash
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install hash
Provenance and assurance #
None of the cryptographic kernels here was written for this package. Each
one comes from upstream C or is copied unchanged from production code. The
import is pinned to one upstream commit and can be reproduced byte for byte
with scripts/import.ml. Every vendored file carries a provenance stamp, and
the importer reads the exact blobs recorded at the pinned commit instead of a
checkout.
The hardware SHA-1, SHA-256 and SHA-512 code in lib/c/native/awslc/ is the
ARMv8 assembly that AWS-LC maintains and
pre-generates, from Andy Polyakov's OpenSSL perlasm as shipped in BoringSSL
and AWS-LC. The SHA-384/512 AArch64 assembly is the implementation that
aws-lc-verification
machine-checks by NSym symbolic simulation against a SHA-2 specification,
with the caveats documented there. SHA-1 and SHA-256 have no formal proof.
The case for them rests on their use in TLS at AWS scale and on this
package's own cross-checks.
The portable C in lib/c/native/*.c comes from digestif: Vincent Hanquez's
code and the BLAKE2b reference implementation. It is scalar and unverified,
and it serves as the reference that the hardware paths are checked against.
The only C written here is lib/c/native/sha_armv8.c, which detects CPU
features at run time (sysctl or hwcaps) and dispatches. It holds no rounds,
no message schedule and no other cryptographic logic.
The pure-OCaml backend in lib/ocaml/baijiu_*.ml is the path js_of_ocaml
uses, with no C at all. It is digestif's baijiu core, copied verbatim. Its
digests are compared byte for byte with the C backend over the same vectors
in test/backend, which both test/c and test/ocaml run.
The tests run the FIPS 180-4 and RFC 1321 digest vectors, the RFC 7693 BLAKE2b vectors and the RFC 4231 and RFC 2202 HMAC vectors on whichever path the machine dispatches to. A cross-check then runs the test binary again on the portable path, over inputs that straddle block boundaries, and demands the same digests for all three accelerated SHAs. A dispatch test fails when a CPU known to have an extension does not report the accelerated path.
Usage #
Hash a string in one call or feed it in pieces; the digest is the same. HMAC takes the key as a labelled argument:
# Hash.SHA256.(to_hex (digest_string "hello"));;
- : string =
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
# let ctx = Hash.SHA256.init ();;
val ctx : Hash.SHA256.ctx = <abstr>
# let ctx = Hash.SHA256.feed_string ctx "hel";;
val ctx : Hash.SHA256.ctx = <abstr>
# let ctx = Hash.SHA256.feed_string ctx "lo";;
val ctx : Hash.SHA256.ctx = <abstr>
# Hash.SHA256.(to_hex (get ctx));;
- : string =
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
# Hash.SHA256.(to_hex (hmac_string ~key:"secret" "message"));;
- : string =
"8b5f48702995c1598c573db1e21866a9b825d4a794d169d7060a03605796360b"
reads wraps a Bytesrw reader so that the bytes are hashed as they pass
through, with no extra buffer. The digest is ready once the reader is
drained:
# let r = Bytesrw.Bytes.Reader.of_string "hello";;
val r : Bytesrw.Bytes.Reader.t = <abstr>
# let r, digest = Hash.SHA256.reads r;;
val r : Bytesrw.Bytes.Reader.t = <abstr>
val digest : unit -> Hash.SHA256.t = <fun>
# let contents = Bytesrw.Bytes.Reader.to_string r;;
val contents : string = "hello"
# Hash.SHA256.to_hex (digest ());;
- : string =
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
Tests #
The vectors come from the FIPS 180-4 appendix (RFC 1321 for MD5 and RFC 7693
for BLAKE2b) and from RFC 4231 and RFC 2202 for HMAC, and were cross-checked
against Python's hashlib. The suite runs twice, once on the hardware path
and once with HASH_NO_SIMD=1, so both branches of the dispatch are tested.
Bench #
dune exec bench/bench_hash.exe # hardware where available
HASH_NO_SIMD=1 dune exec bench/bench_hash.exe # portable C
License #
MIT AND BSD-2-Clause AND ISC: the OCaml code is MIT, the portable C code carries a BSD-2-Clause notice and the C stubs an ISC one. See LICENSE.md.