f2fs_reader/
fsverity.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4use crate::superblock::BLOCK_SIZE;
5use anyhow::Error;
6use fidl_fuchsia_io as fio;
7use fsverity_merkle::FsVerityDescriptor as DecodedDescriptor;
8
9/// Found in the last block of fsverity data.
10pub struct FsVerityDescriptor<'a> {
11    pub file_size: u64,
12    pub algorithm: fio::HashAlgorithm,
13    pub root: &'a [u8],
14    pub salt: &'a [u8],
15}
16
17impl<'a> FsVerityDescriptor<'a> {
18    /// Parses out the descriptor from bytes.
19    pub fn from_bytes(data: &'a [u8]) -> Result<Self, Error> {
20        let descriptor = DecodedDescriptor::from_bytes(data, BLOCK_SIZE)?;
21
22        Ok(Self {
23            file_size: descriptor.file_size() as u64,
24            algorithm: descriptor.digest_algorithm(),
25            root: descriptor.root_digest(),
26            salt: descriptor.salt(),
27        })
28    }
29
30    /// Create a fuchsia.io VerificationOptions to match this descriptor.
31    pub fn fio_verification_options(&self) -> fio::VerificationOptions {
32        fio::VerificationOptions {
33            hash_algorithm: Some(self.algorithm),
34            salt: Some(self.salt.to_vec()),
35            ..Default::default()
36        }
37    }
38}