bpak_merkle — BPAK merkle hash tree

This documents the BPAK merkle hash tree generator which is used to create dm verity compatiable hash trees.


Source code: include/bpak/merkle.h, lib/merkle.c


BPAK - Bit Packer

Copyright (C) 2022 Jonas Blixt jonpe960@gmail.com

SPDX-License-Identifier: BSD-3-Clause

Defines

BPAK_MERKLE_BLOCK_SZ
BPAK_MERKLE_MAX_LEVELS
BPAK_MERKLE_BLOCK_BITS
BPAK_MERKLE_HASH_BYTES

Typedefs

typedef unsigned char bpak_merkle_hash_t[BPAK_MERKLE_HASH_BYTES]

Only sha256 is used in this implementation

Functions

ssize_t bpak_merkle_compute_size(size_t input_data_length)
size_t bpak_merkle_get_size(struct bpak_merkle_context *ctx)

Returns the total size in bytes of the hash tree

Parameters:

ctx[in] Context

Returns:

Size in bytes

int bpak_merkle_init(struct bpak_merkle_context *ctx, size_t input_data_length, const uint8_t *salt, size_t salt_length, bpak_io_t wr, bpak_io_t rd, off_t offset, bool zero_fill_output, void *priv)

Initializes the merkle algorithm

Parameters:
  • ctx[in] Context

  • input_data_length[in] Size of filesystem in bytes

  • salt[in] Salt to use for hash computation

  • salt_length[in] Length of salt in bytes

  • wr[in] Write callback function

  • rd[in] Read callback function

  • offset[in] Offset where hash tree data starts

  • priv[in] Optional private context

Returns:

BPAK_OK on success and non zero number on error

int bpak_merkle_write_chunk(struct bpak_merkle_context *ctx, uint8_t *buffer, size_t length)

Process input data stream. This function can also be called with no buffer and sz set to zero to complete the hash tree computation.

Parameters:
  • ctx[in] Context

  • buffer[in] Input data buffer

  • length[in] Available bytes in buffer

Returns:

BPAK_OK on success

int bpak_merkle_finish(struct bpak_merkle_context *ctx, bpak_merkle_hash_t roothash)

Outputs the root hash when the tree is computed

Parameters:
  • ctx[in] Context

  • roothash[out] Root hash output

Returns:

BPAK_OK on success

struct bpak_merkle_context

Public Members

struct bpak_hash_context running_hash
uint8_t buffer[BPAK_MERKLE_HASH_BYTES]
size_t level_length[BPAK_MERKLE_MAX_LEVELS]
off_t level_offset[BPAK_MERKLE_MAX_LEVELS]
unsigned int no_of_levels
size_t input_chunk_counter
size_t block_byte_counter
size_t input_data_length

Total size of the input filesystem in bytes

size_t hash_tree_length

Hash tree output size

size_t salt_length
bool finished
uint8_t salt[32]

Input salt for hashing

bpak_io_t wr

Function to write to the hash tree

bpak_io_t rd

Function to read from the hash tree

off_t offset
void *priv

Externalt context variable