blob: cb268efdf0d39f61a3a4b4251c65d09fe6669fc0 [file] [log] [blame]
Victor Hsiehdde17902021-02-26 12:35:31 -08001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::io;
18
19use thiserror::Error;
20
21use super::sys::{FS_VERITY_HASH_ALG_SHA256, FS_VERITY_LOG_BLOCKSIZE, FS_VERITY_VERSION};
22use crate::common::{divide_roundup, CHUNK_SIZE};
Andrew Scull761db1e2022-05-23 18:31:35 +000023use openssl::sha::Sha256;
24
25/// Output size of SHA-256 in bytes.
26pub const SHA256_HASH_SIZE: usize = 32;
27
28/// A SHA-256 hash.
29pub type Sha256Hash = [u8; SHA256_HASH_SIZE];
Victor Hsiehdde17902021-02-26 12:35:31 -080030
31#[derive(Error, Debug)]
32pub enum FsverityError {
Victor Hsieh5deba522022-01-10 17:18:40 -080033 #[error("Invalid digest")]
34 InvalidDigest,
Victor Hsiehdde17902021-02-26 12:35:31 -080035 #[error("Insufficient data, only got {0}")]
36 InsufficientData(usize),
37 #[error("Cannot verify a block")]
38 CannotVerify,
39 #[error("I/O error")]
40 Io(#[from] io::Error),
Victor Hsiehdde17902021-02-26 12:35:31 -080041 #[error("Invalid state")]
42 InvalidState,
43}
44
45fn log128_ceil(num: u64) -> Option<u64> {
46 match num {
47 0 => None,
48 n => Some(divide_roundup(64 - (n - 1).leading_zeros() as u64, 7)),
49 }
50}
51
52/// Return the Merkle tree height for our tree configuration, or None if the size is 0.
53pub fn merkle_tree_height(data_size: u64) -> Option<u64> {
Andrew Scull761db1e2022-05-23 18:31:35 +000054 let hashes_per_node = CHUNK_SIZE / SHA256_HASH_SIZE as u64;
Victor Hsiehdde17902021-02-26 12:35:31 -080055 let hash_pages = divide_roundup(data_size, hashes_per_node * CHUNK_SIZE);
56 log128_ceil(hash_pages)
57}
58
Victor Hsieh35dfa1e2022-01-12 17:03:35 -080059/// Returns the size of Merkle tree for `data_size` bytes amount of data.
60pub fn merkle_tree_size(mut data_size: u64) -> u64 {
61 let mut total = 0;
62 while data_size > CHUNK_SIZE {
Andrew Scull761db1e2022-05-23 18:31:35 +000063 let hash_size = divide_roundup(data_size, CHUNK_SIZE) * SHA256_HASH_SIZE as u64;
Victor Hsieh35dfa1e2022-01-12 17:03:35 -080064 let hash_storage_size = divide_roundup(hash_size, CHUNK_SIZE) * CHUNK_SIZE;
65 total += hash_storage_size;
66 data_size = hash_storage_size;
67 }
68 total
69}
70
Andrew Scull761db1e2022-05-23 18:31:35 +000071pub fn build_fsverity_digest(root_hash: &Sha256Hash, file_size: u64) -> Sha256Hash {
Victor Hsiehdde17902021-02-26 12:35:31 -080072 // Little-endian byte representation of fsverity_descriptor from linux/fsverity.h
73 // Not FFI-ed as it seems easier to deal with the raw bytes manually.
Andrew Scull761db1e2022-05-23 18:31:35 +000074 let mut hash = Sha256::new();
75 hash.update(&FS_VERITY_VERSION.to_le_bytes()); // version
76 hash.update(&FS_VERITY_HASH_ALG_SHA256.to_le_bytes()); // hash_algorithm
77 hash.update(&FS_VERITY_LOG_BLOCKSIZE.to_le_bytes()); // log_blocksize
78 hash.update(&0u8.to_le_bytes()); // salt_size
79 hash.update(&0u32.to_le_bytes()); // sig_size
80 hash.update(&file_size.to_le_bytes()); // data_size
81 hash.update(root_hash); // root_hash, first 32 bytes
82 hash.update(&[0u8; 32]); // root_hash, last 32 bytes, always 0 because we are using sha256.
83 hash.update(&[0u8; 32]); // salt
84 hash.update(&[0u8; 32]); // reserved
85 hash.update(&[0u8; 32]); // reserved
86 hash.update(&[0u8; 32]); // reserved
87 hash.update(&[0u8; 32]); // reserved
88 hash.update(&[0u8; 16]); // reserved
89 hash.finish()
Victor Hsiehdde17902021-02-26 12:35:31 -080090}
Victor Hsieh35dfa1e2022-01-12 17:03:35 -080091
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 #[test]
97 fn test_merkle_tree_size() {
98 // To produce groundtruth:
99 // dd if=/dev/zero of=zeros bs=1 count=524289 && \
100 // fsverity digest --out-merkle-tree=tree zeros && \
101 // du -b tree
102 assert_eq!(merkle_tree_size(0), 0);
103 assert_eq!(merkle_tree_size(1), 0);
104 assert_eq!(merkle_tree_size(4096), 0);
105 assert_eq!(merkle_tree_size(4097), 4096);
106 assert_eq!(merkle_tree_size(524288), 4096);
107 assert_eq!(merkle_tree_size(524289), 12288);
108 }
109}