1 /*
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 
17 use std::io;
18 
19 use thiserror::Error;
20 
21 use super::sys::{FS_VERITY_HASH_ALG_SHA256, FS_VERITY_LOG_BLOCKSIZE, FS_VERITY_VERSION};
22 use crate::common::{divide_roundup, CHUNK_SIZE};
23 use crate::crypto::{CryptoError, Sha256Hash, Sha256Hasher};
24 
25 #[derive(Error, Debug)]
26 pub enum FsverityError {
27     #[error("Cannot verify a signature")]
28     BadSignature,
29     #[error("Insufficient data, only got {0}")]
30     InsufficientData(usize),
31     #[error("Cannot verify a block")]
32     CannotVerify,
33     #[error("I/O error")]
34     Io(#[from] io::Error),
35     #[error("Crypto")]
36     UnexpectedCryptoError(#[from] CryptoError),
37     #[error("Invalid state")]
38     InvalidState,
39 }
40 
log128_ceil(num: u64) -> Option<u64>41 fn log128_ceil(num: u64) -> Option<u64> {
42     match num {
43         0 => None,
44         n => Some(divide_roundup(64 - (n - 1).leading_zeros() as u64, 7)),
45     }
46 }
47 
48 /// Return the Merkle tree height for our tree configuration, or None if the size is 0.
merkle_tree_height(data_size: u64) -> Option<u64>49 pub fn merkle_tree_height(data_size: u64) -> Option<u64> {
50     let hashes_per_node = CHUNK_SIZE / Sha256Hasher::HASH_SIZE as u64;
51     let hash_pages = divide_roundup(data_size, hashes_per_node * CHUNK_SIZE);
52     log128_ceil(hash_pages)
53 }
54 
build_fsverity_digest( root_hash: &Sha256Hash, file_size: u64, ) -> Result<Sha256Hash, CryptoError>55 pub fn build_fsverity_digest(
56     root_hash: &Sha256Hash,
57     file_size: u64,
58 ) -> Result<Sha256Hash, CryptoError> {
59     // Little-endian byte representation of fsverity_descriptor from linux/fsverity.h
60     // Not FFI-ed as it seems easier to deal with the raw bytes manually.
61     Sha256Hasher::new()?
62         .update(&FS_VERITY_VERSION.to_le_bytes())? // version
63         .update(&FS_VERITY_HASH_ALG_SHA256.to_le_bytes())? // hash_algorithm
64         .update(&FS_VERITY_LOG_BLOCKSIZE.to_le_bytes())? // log_blocksize
65         .update(&0u8.to_le_bytes())? // salt_size
66         .update(&0u32.to_le_bytes())? // sig_size
67         .update(&file_size.to_le_bytes())? // data_size
68         .update(root_hash)? // root_hash, first 32 bytes
69         .update(&[0u8; 32])? // root_hash, last 32 bytes, always 0 because we are using sha256.
70         .update(&[0u8; 32])? // salt
71         .update(&[0u8; 32])? // reserved
72         .update(&[0u8; 32])? // reserved
73         .update(&[0u8; 32])? // reserved
74         .update(&[0u8; 32])? // reserved
75         .update(&[0u8; 16])? // reserved
76         .finalize()
77 }
78