dm-verity hash trees (Linux device-mapper verity)
README.md

verity #

dm-verity hash trees in pure OCaml. The Linux device-mapper is the block layer that presents a virtual block device built from real ones, and a device-mapper target is the transformation it applies on the way. dm-verity is the target that gives a read-only block device tamper-evidence: the device is split into fixed-size blocks, each block is hashed, the hashes are packed into hash blocks and hashed again, recursively, up to a single root hash. On every read of a data block, the kernel hashes it, checks the digest against its slot in the hash block above, and climbs the same way through the hash blocks it has not verified yet, up to the root hash given at setup (usually signed and checked at boot); it fails the read with an I/O error on any mismatch. Knowing the small root hash therefore lets the kernel verify any data block on demand without trusting the storage.

This library computes that hash tree, in the format produced by veritysetup (cryptsetup): format 1 (salt prepended), digest slots padded to a power of two, hash blocks zero-padded. Each data block's digest is H(salt || block). Digests are written into hash blocks in slots rounded up to a power of two (32 bytes for SHA-256, so a = 128 slots per 4 KiB block), with the unused tail filled with zeros. Each hash block is hashed the same way, H(salt || hash block), into the level above, until one block remains; its salted hash is the root hash.

The tree's size follows. A device of n data blocks of B bytes has ceil(n / a) hash blocks at the lowest level, ceil(n / a^2) above, and so on up to one, L = ceil(log_a n) levels in all, and veritysetup puts a superblock padded to one block in front. A 1 GiB image in 4 KiB blocks has n = 262,144, so its levels hold 2048, 16 and 1 hash blocks, and the hash device is (1 + 2048 + 16 + 1) x 4096 = 8,462,336 bytes, 0.8% of the data, which Verity.hash_device_size cfg ~data_blocks:262144 returns without hashing anything. A read checks at most L = 3 hash blocks above the data block.

A configuration fixes the digest algorithm (SHA256 by default, also SHA1/SHA512), the salt, and the block sizes (4096 by default). Verity.root returns the raw root hash of an image (a whole multiple of the data block size); Verity.hash_device returns the full on-disk hash device (superblock + tree, byte-identical to veritysetup format); and Verity.verify_block performs the kernel's read-time check of one block:

# let cfg = Verity.v ~salt:(String.make 32 '\000') ()
val cfg : Verity.t = <abstr>
# String.length (Verity.root cfg (String.make 16384 'A'))
- : int = 32
# String.length (Verity.hash_device cfg (String.make 16384 'A'))
- : int = 8192
# let data = String.make 16384 'A' in
  let hash_device = Verity.hash_device cfg data in
  let root = Verity.root cfg data in
  Verity.verify_block cfg ~data_blocks:4 ~hash_device ~root 0
    (String.sub data 0 4096)
- : bool = true

The 16384 bytes of the example are four 4 KiB data blocks. Their four digests fit in one hash block, which is the root level, so the hash device is that block behind the 4096-byte superblock: 8192 bytes.

The block count, like the root, comes from the record that names the device, as the kernel's dm-verity table carries <num_data_blocks> beside the root digest. The verifier never reads it from the hash device: the superblock must agree with it, and the hash device must be exactly the size it gives. A hash device whose superblock claims fewer blocks, cut to the top of the real tree, is a whole tree for that count under the same root, and would let its hash blocks verify as data blocks.

An image too large to hold in memory can be hashed as it streams: Verity.hasher starts a context, Verity.feed adds bytes in whatever pieces the source delivers, and Verity.finish returns the same root hash and hash device as the whole-image functions.

Verity.Fsverity computes the fs-verity file digest, the value fsverity digest prints for a file, over the same kind of tree built per file. fs-verity is the per-file counterpart built into ext4, f2fs and btrfs: the filesystem keeps a Merkle tree beside one read-only file and checks each page as it is read, and the file's digest is the SHA-256 of a 256-byte descriptor holding the tree's root, the file size, the block size and the salt. With no salt a leaf is the plain SHA-256 of a 4 KiB page, so the tree also names each page by its content: Fsverity.update recomputes only the paths of changed pages, and Fsverity.want lists the pages and hash blocks the holder of one tree lacks of another.

Installation #

Install with opam:

$ opam install verity

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 verity

Verified against veritysetup #

The tests pin the root hash against veritysetup format output for fixed, deterministic inputs (single-level and multi-level trees). Regenerate the vectors with:

veritysetup format --salt=<hex> --hash=sha256 \
  --data-block-size=4096 --hash-block-size=4096 data.img hash.img

Licence #

ISC.