Skip to content

An authenticated manifest

Three independent choices, combined: keyed mode, file input and verification. Together they make a checksum list that only a holder of the key can produce or trust.

Try it on Compiler Explorer

auto tag = blake3pp::hash_file(path, {.mode = blake3pp::hash_mode::keyed(key)});
...
if (!recomputed.matches(expected_hex)) { /* reject */ }

What the key buys

A plain checksum list detects accidental corruption. It does not detect a deliberate edit, because anyone who changes a file can recompute its line and the list still verifies.

Keying closes that. Each line depends on a secret, so producing a correct line for altered contents needs the key. The example shows both cases: a tampered file fails against its old line, and a line computed without the key is rejected.

This is what blake3ppsum --keyed does, and the reason the tool reads its key from a file rather than from the command line.

Verification

matches() recomputes the comparison against hex in one step. It is constant-time, like every digest comparison in the library, and hex that does not parse counts as a mismatch rather than raising. Where you want the parse to be explicit, digest::from_hex() returns a std::optional<digest> that compares directly against a digest.

Where the key comes from

The key here is generated in the example so that it runs anywhere. A real one comes from a key store or a key-derivation step — see 03-keyed-and-derive-key for derive_key, which turns one master secret into purpose-bound subkeys.

hash_file_options carries one mode, named as hasher names the same choice: hash_mode::keyed(key) or hash_mode::derive_key(context). All three modes are therefore hash_file()'s, and no options object can ask for two at once.

Extended output has no shortcut there, since the digest it returns is 32 bytes. Take the hasher the options describe with make_hasher() and stream it with update_file(), as 05-hash-a-file does.

Running it

cmake --build build --target blake3pp_example_10_authenticated_manifest
./build/examples/blake3pp_example_10_authenticated_manifest

Next

  • The API lays out every one of these choices and what it does not constrain.

The whole program

// Three choices that compose: keyed mode, file input, and verification.
// Together they are an authenticated manifest -- a checksum list that
// only the holder of the key can produce or trust.

#include <blake3pp/blake3pp.hpp>

#include <array>
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace {

std::array<std::byte, blake3pp::key_size> demo_key() {
  std::array<std::byte, blake3pp::key_size> key{};
  for (std::size_t i = 0; i < key.size(); ++i) {
    key[i] = static_cast<std::byte>(0xA0 + i);
  }
  return key;
}

void write_file(const std::filesystem::path& path, std::string_view text) {
  std::ofstream out(path, std::ios::binary);
  out << text;
}

}  // namespace

int main() {
  const auto dir = std::filesystem::temp_directory_path() / "blake3pp-example-10";
  std::filesystem::create_directories(dir);
  for (const auto& [name, text] : {std::pair{"alpha.txt", "the first payload"},
                                   std::pair{"beta.txt", "the second payload"},
                                   std::pair{"gamma.txt", "the third payload"}}) {
    write_file(dir / name, text);
  }

  const auto key = demo_key();
  const std::vector<std::string> names = {"alpha.txt", "beta.txt", "gamma.txt"};

  // Building the manifest: a keyed hash per file. The key rides in the
  // options, so this is still one call per file.
  std::vector<std::pair<std::string, std::string>> manifest;
  std::cout << "manifest\n";
  for (const auto& name : names) {
    const auto tag = blake3pp::hash_file(dir / name, {.mode = blake3pp::hash_mode::keyed(key)});
    manifest.emplace_back(name, tag.to_hex());
    std::cout << "  " << tag.to_hex() << "  " << name << '\n';
  }

  // Verifying it: recompute and compare. matches() does both steps, in
  // constant time, and treats unparseable hex as a mismatch rather than
  // an error.
  std::cout << "\nverifying\n";
  for (const auto& [name, expected] : manifest) {
    const auto actual = blake3pp::hash_file(dir / name, {.mode = blake3pp::hash_mode::keyed(key)});
    std::cout << "  " << name << ": "
              << (actual.matches(expected) ? "ok" : "FAILED") << '\n';
  }

  // A modified file fails, which a plain checksum would also catch...
  write_file(dir / "beta.txt", "the second payload, tampered with");
  const auto tampered = blake3pp::hash_file(dir / "beta.txt", {.mode = blake3pp::hash_mode::keyed(key)});
  std::cout << "\nafter editing beta.txt: "
            << (tampered.matches(manifest[1].second) ? "ok" : "FAILED") << '\n';

  // ...and so does a manifest rewritten by someone without the key, which
  // a plain checksum would not. Without the key, no correct line can be
  // produced for the new contents.
  const auto forged = blake3pp::hash_file(dir / "beta.txt");   // unkeyed
  std::cout << "a line computed without the key: "
            << (forged.matches(tampered.to_hex()) ? "accepted" : "rejected")
            << '\n';

  std::filesystem::remove_all(dir);
}