Skip to main content

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    descriptor: DecodedDescriptor<'a>,
14}
15
16impl<'a> FsVerityDescriptor<'a> {
17    /// Parses out the descriptor from bytes.
18    pub fn from_bytes(data: &'a [u8]) -> Result<Self, Error> {
19        let descriptor = DecodedDescriptor::new(data, BLOCK_SIZE)?;
20
21        Ok(Self {
22            file_size: descriptor.file_size() as u64,
23            algorithm: descriptor.digest_algorithm(),
24            descriptor,
25        })
26    }
27
28    pub fn root(&self) -> &[u8] {
29        self.descriptor.root_digest()
30    }
31
32    pub fn salt(&self) -> &[u8] {
33        self.descriptor.salt()
34    }
35
36    /// Create a fuchsia.io VerificationOptions to match this descriptor.
37    pub fn fio_verification_options(&self) -> fio::VerificationOptions {
38        fio::VerificationOptions {
39            hash_algorithm: Some(self.algorithm),
40            salt: Some(self.descriptor.salt().to_vec()),
41            ..Default::default()
42        }
43    }
44}