blob: dd1bd042ca3baca9ce2a50bce62d69755286cdb9 [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 super::common::{build_fsverity_digest, merkle_tree_height, FsverityError};
18use crate::common::CHUNK_SIZE;
19use crate::crypto::{CryptoError, Sha256Hash, Sha256Hasher};
20
21const HASH_SIZE: usize = Sha256Hasher::HASH_SIZE;
22const HASH_PER_PAGE: usize = CHUNK_SIZE as usize / HASH_SIZE;
23
24/// MerkleLeaves can be used by the class' customer for bookkeeping integrity data for their bytes.
25/// It can also be used to generate the standard fs-verity digest for the source data.
26///
27/// It's in-memory because for the initial use cases, we don't need to read back an existing file,
28/// and only need to deal with new files. Also, considering that the output file won't be large at
29/// the moment, it is sufficient to simply keep the Merkle tree in memory in the trusted world. To
30/// further simplify the initial implementation, we only need to keep the leaf nodes in memory, and
31/// generate the tree / root hash when requested.
32pub struct MerkleLeaves {
33 leaves: Vec<Sha256Hash>,
34 file_size: u64,
35}
36
37fn hash_all_pages(source: &[Sha256Hash]) -> Result<Vec<Sha256Hash>, CryptoError> {
38 source
39 .chunks(HASH_PER_PAGE)
40 .map(|chunk| {
41 let padding_bytes = (HASH_PER_PAGE - chunk.len()) * HASH_SIZE;
Victor Hsieh6cf75b52021-04-01 12:45:49 -070042 Sha256Hasher::new()?.update_from(chunk)?.update(&vec![0u8; padding_bytes])?.finalize()
Victor Hsiehdde17902021-02-26 12:35:31 -080043 })
44 .collect()
45}
46
Victor Hsiehdde17902021-02-26 12:35:31 -080047impl MerkleLeaves {
48 /// Creates a `MerkleLeaves` instance with empty data.
49 pub fn new() -> Self {
50 Self { leaves: Vec::new(), file_size: 0 }
51 }
52
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080053 /// Gets size of the file represented by `MerkleLeaves`.
54 pub fn file_size(&self) -> u64 {
55 self.file_size
56 }
57
Victor Hsiehdde17902021-02-26 12:35:31 -080058 /// Updates the hash of the `index`-th leaf, and increase the size to `size_at_least` if the
59 /// current size is smaller.
60 pub fn update_hash(&mut self, index: usize, hash: &Sha256Hash, size_at_least: u64) {
61 // +1 since index is zero-based.
62 if self.leaves.len() < index + 1 {
63 // When resizing, fill in hash of zeros by default. This makes it easy to handle holes
64 // in a file.
65 self.leaves.resize(index + 1, Sha256Hasher::HASH_OF_4096_ZEROS);
66 }
67 self.leaves[index].clone_from_slice(hash);
68
69 if size_at_least > self.file_size {
70 self.file_size = size_at_least;
71 }
72 }
73
74 /// Returns whether `index` is within the bound of leaves.
75 pub fn is_index_valid(&self, index: usize) -> bool {
76 index < self.leaves.len()
77 }
78
79 /// Returns whether the `index`-th hash is consistent to `hash`.
80 pub fn is_consistent(&self, index: usize, hash: &Sha256Hash) -> bool {
81 if let Some(element) = self.leaves.get(index) {
82 element == hash
83 } else {
84 false
85 }
86 }
87
88 fn calculate_root_hash(&self) -> Result<Sha256Hash, FsverityError> {
89 match self.leaves.len() {
90 // Special cases per fs-verity digest definition.
91 0 => {
92 debug_assert_eq!(self.file_size, 0);
93 Ok([0u8; HASH_SIZE])
94 }
95 1 => {
96 debug_assert!(self.file_size <= CHUNK_SIZE && self.file_size > 0);
97 Ok(self.leaves[0])
98 }
99 n => {
100 debug_assert_eq!((self.file_size - 1) / CHUNK_SIZE, n as u64);
101 let size_for_equivalent = n as u64 * CHUNK_SIZE;
102 let level = merkle_tree_height(size_for_equivalent).unwrap(); // safe since n > 0
103
104 // `leaves` is owned and can't be the initial state below. Here we manually hash it
105 // first to avoid a copy and to get the type right.
106 let second_level = hash_all_pages(&self.leaves)?;
107 let hashes =
108 (1..=level).try_fold(second_level, |source, _| hash_all_pages(&source))?;
109 if hashes.len() != 1 {
110 Err(FsverityError::InvalidState)
111 } else {
112 Ok(hashes.into_iter().next().unwrap())
113 }
114 }
115 }
116 }
117
118 /// Returns the fs-verity digest based on the current tree and file size.
119 pub fn calculate_fsverity_digest(&self) -> Result<Sha256Hash, FsverityError> {
120 let root_hash = self.calculate_root_hash()?;
121 Ok(build_fsverity_digest(&root_hash, self.file_size)?)
122 }
123}
124
125#[cfg(test)]
126mod tests {
127 // Test data below can be generated by:
128 // $ perl -e 'print "\x{00}" x 6000' > foo
129 // $ perl -e 'print "\x{01}" x 5000' >> foo
130 // $ fsverity digest foo
131 use super::*;
132 use anyhow::Result;
133
134 #[test]
135 fn merkle_tree_empty_file() -> Result<()> {
136 assert_eq!(
137 to_u8_vec("3d248ca542a24fc62d1c43b916eae5016878e2533c88238480b26128a1f1af95"),
138 generate_fsverity_digest_sequentially(&Vec::new())?
139 );
140 Ok(())
141 }
142
143 #[test]
144 fn merkle_tree_file_size_less_than_or_equal_to_4k() -> Result<()> {
145 // Test a file that contains 4096 '\01's.
146 assert_eq!(
147 to_u8_vec("cd0875ca59c7d37e962c5e8f5acd3770750ac80225e2df652ce5672fd34500af"),
148 generate_fsverity_digest_sequentially(&vec![1; 4096])?
149 );
150 Ok(())
151 }
152
153 #[test]
154 fn merkle_tree_more_sizes() -> Result<()> {
155 // Test files that contains >4096 '\01's.
156
157 assert_eq!(
158 to_u8_vec("2901b849fda2d91e3929524561c4a47e77bb64734319759507b2029f18b9cc52"),
159 generate_fsverity_digest_sequentially(&vec![1; 4097])?
160 );
161
162 assert_eq!(
163 to_u8_vec("2a476d58eb80394052a3a783111e1458ac3ecf68a7878183fed86ca0ff47ec0d"),
164 generate_fsverity_digest_sequentially(&vec![1; 8192])?
165 );
166
167 // Test with max size that still fits in 2 levels.
168 assert_eq!(
169 to_u8_vec("26b7c190a34e19f420808ee7ec233b09fa6c34543b5a9d2950530114c205d14f"),
170 generate_fsverity_digest_sequentially(&vec![1; 524288])?
171 );
172
173 // Test with data that requires 3 levels.
174 assert_eq!(
175 to_u8_vec("316835d9be1c95b5cd55d07ae7965d651689efad186e26cbf680e40b683a3262"),
176 generate_fsverity_digest_sequentially(&vec![1; 524289])?
177 );
178 Ok(())
179 }
180
181 #[test]
182 fn merkle_tree_non_sequential() -> Result<()> {
183 let mut tree = MerkleLeaves::new();
184 let hash = Sha256Hasher::new()?.update(&vec![1u8; CHUNK_SIZE as usize])?.finalize()?;
185
186 // Update hashes of 4 1-blocks.
187 tree.update_hash(1, &hash, CHUNK_SIZE * 2);
188 tree.update_hash(3, &hash, CHUNK_SIZE * 4);
189 tree.update_hash(0, &hash, CHUNK_SIZE);
190 tree.update_hash(2, &hash, CHUNK_SIZE * 3);
191
192 assert_eq!(
193 to_u8_vec("7d3c0d2e1dc54230b20ed875f5f3a4bd3f9873df601936b3ca8127d4db3548f3"),
194 tree.calculate_fsverity_digest()?
195 );
196 Ok(())
197 }
198
199 fn generate_fsverity_digest_sequentially(test_data: &[u8]) -> Result<Sha256Hash> {
200 let mut tree = MerkleLeaves::new();
201 for (index, chunk) in test_data.chunks(CHUNK_SIZE as usize).enumerate() {
202 let hash = Sha256Hasher::new()?
203 .update(&chunk)?
204 .update(&vec![0u8; CHUNK_SIZE as usize - chunk.len()])?
205 .finalize()?;
206
207 tree.update_hash(index, &hash, CHUNK_SIZE * index as u64 + chunk.len() as u64);
208 }
209 Ok(tree.calculate_fsverity_digest()?)
210 }
211
212 fn to_u8_vec(hex_str: &str) -> Vec<u8> {
213 assert!(hex_str.len() % 2 == 0);
214 (0..hex_str.len())
215 .step_by(2)
216 .map(|i| u8::from_str_radix(&hex_str[i..i + 2], 16).unwrap())
217 .collect()
218 }
219}