Android-cuttlefish cvd tool
fec_private.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 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#ifndef __FEC_PRIVATE_H__
18#define __FEC_PRIVATE_H__
19
20#include <errno.h>
21#include <fcntl.h>
22#include <pthread.h>
23#include <stdio.h>
24#include <string.h>
25#include <sys/syscall.h>
26#include <unistd.h>
27
28#include <memory>
29#include <string>
30#include <vector>
31
32#include <android-base/threads.h>
33
34#define STRINGIFY_INNER(x) #x
35#define STRINGIFY(x) STRINGIFY_INNER(x)
36#include <crypto_utils/android_pubkey.h>
37#include <fec/ecc.h>
38#include <fec/io.h>
39#include <openssl/obj_mac.h>
40#include <openssl/sha.h>
41#include <utils/Compat.h>
42
43/* processing parameters */
44#define WORK_MIN_THREADS 1
45#define WORK_MAX_THREADS 64
46
47/* verity parameters */
48#define VERITY_CACHE_BLOCKS 4096
49#define VERITY_NO_CACHE UINT64_MAX
50
51/* verity definitions */
52#define VERITY_METADATA_SIZE (8 * FEC_BLOCKSIZE)
53#define VERITY_TABLE_ARGS 10 /* mandatory arguments */
54#define VERITY_MIN_TABLE_SIZE (VERITY_TABLE_ARGS * 2) /* for quick validation */
55#define VERITY_MAX_TABLE_SIZE (VERITY_METADATA_SIZE - sizeof(verity_header))
56
57/* verity header and metadata */
58#define VERITY_MAGIC 0xB001B001
59#define VERITY_MAGIC_DISABLE 0x46464F56
60#define VERITY_VERSION 0
61#define VERITY_TABLE_FIELDS 10
62#define VERITY_TABLE_VERSION 1
63
65 uint32_t magic;
66 uint32_t version;
68 uint32_t length;
69};
70
71/* file handle */
72struct ecc_info {
73 bool valid;
74 int roots;
75 int rsn;
76 uint32_t size;
77 uint64_t blocks;
78 uint64_t rounds;
79 uint64_t start; /* offset in file */
80};
81
83 // The number of the input data blocks to compute the hashtree.
84 uint64_t data_blocks;
85 // The offset of hashtree in the final image.
86 uint64_t hash_start;
87 // The hash concatenation of the input data, i.e. lowest level of the
88 // hashtree.
89 std::vector<uint8_t> hash_data;
90 std::vector<uint8_t> salt;
91 std::vector<uint8_t> zero_hash;
92
93 // Initialize the hashtree offsets and properties with the input parameters.
94 int initialize(uint64_t hash_start, uint64_t data_blocks,
95 const std::vector<uint8_t> &salt, int nid);
96
97 // Checks if the bytes in 'block' has the expected hash. And the 'index' is
98 // the block number of is the input block in the filesystem.
99 bool check_block_hash_with_index(uint64_t index, const uint8_t *block);
100
101 // Reads the verity hash tree, validates it against the root hash in `root',
102 // corrects errors if necessary, and copies valid data blocks for later use
103 // to 'hashtree'.
104 int verify_tree(const fec_handle *f, const uint8_t *root);
105
106 private:
107 bool ecc_read_hashes(fec_handle *f, uint64_t hash_offset, uint8_t *hash,
108 uint64_t data_offset, uint8_t *data);
109
110 // Computes the hash for FEC_BLOCKSIZE bytes from buffer 'block' and
111 // compares it to the expected value in 'expected'.
112 bool check_block_hash(const uint8_t *expected, const uint8_t *block);
113
114 // Computes the hash of 'block' and put the result in 'hash'.
115 int get_hash(const uint8_t *block, uint8_t *hash);
116
117 int nid_; // NID for the hash algorithm.
120};
121
124 std::string table;
125 uint64_t metadata_start; /* offset in file */
129};
130
131struct avb_info {
132 bool valid = false;
133 std::vector<uint8_t> vbmeta;
135};
136
139 int fd;
140 int flags; /* additional flags passed to fec_open */
141 int mode; /* mode for open(2) */
142 uint64_t errors;
143 uint64_t data_size;
144 uint64_t pos;
145 uint64_t size;
146 // TODO(xunchang) switch to std::optional
149
151 return avb.valid ? avb.hashtree : verity.hashtree;
152 }
153};
154
155/* I/O helpers */
156extern bool raw_pread(int fd, void *buf, size_t count, uint64_t offset);
157extern bool raw_pwrite(int fd, const void *buf, size_t count, uint64_t offset);
158
159/* processing functions */
160typedef ssize_t (*read_func)(fec_handle *f, uint8_t *dest, size_t count,
161 uint64_t offset, size_t *errors);
162
163extern ssize_t process(fec_handle *f, uint8_t *buf, size_t count,
164 uint64_t offset, read_func func);
165
166/* verity functions */
167extern uint64_t verity_get_size(uint64_t file_size, uint32_t *verity_levels,
168 uint32_t *level_hashes,
169 uint32_t padded_digest_size);
170
171extern int verity_parse_header(fec_handle *f, uint64_t offset);
172
173/* helper macros */
174#ifndef unlikely
175 #define unlikely(x) __builtin_expect(!!(x), 0)
176 #define likely(x) __builtin_expect(!!(x), 1)
177#endif
178
179/* warnings, errors, debug output */
180#ifdef FEC_NO_KLOG
181 #define __log(func, type, format, args...) \
182 fprintf(stderr, "fec: <%" PRIu64 "> " type ": %s: " format "\n", \
183 android::base::GetThreadId(), __FUNCTION__, ##args)
184#else
185 #include <cutils/klog.h>
186
187 #define __log(func, type, format, args...) \
188 KLOG_##func("fec", "<%d> " type ": %s: " format "\n", \
189 (int)syscall(SYS_gettid), __FUNCTION__, ##args)
190#endif
191
192#ifdef NDEBUG
193 #define debug(format, args...)
194#else
195 #define debug(format, args...) __log(DEBUG, "debug", format, ##args)
196#endif
197
198#define warn(format, args...) __log(WARNING, "warning", format, ##args)
199#define error(format, args...) __log(ERROR, "error", format, ##args)
200
201#define check(p) \
202 if (unlikely(!(p))) { \
203 error("`%s' failed", #p); \
204 errno = EFAULT; \
205 return -1; \
206 }
207
208#endif /* __FEC_PRIVATE_H__ */
#define ANDROID_PUBKEY_MODULUS_SIZE
Definition: android_pubkey.h:31
uint8_t hash[SHA256_DIGEST_LENGTH]
Definition: io.h:6
bool raw_pread(int fd, void *buf, size_t count, uint64_t offset)
Definition: fec_read.cpp:475
ssize_t process(fec_handle *f, uint8_t *buf, size_t count, uint64_t offset, read_func func)
Definition: fec_process.cpp:40
ssize_t(* read_func)(fec_handle *f, uint8_t *dest, size_t count, uint64_t offset, size_t *errors)
Definition: fec_private.h:160
uint64_t verity_get_size(uint64_t file_size, uint32_t *verity_levels, uint32_t *level_hashes, uint32_t padded_digest_size)
Definition: fec_verity.cpp:97
int verity_parse_header(fec_handle *f, uint64_t offset)
Definition: fec_verity.cpp:536
bool raw_pwrite(int fd, const void *buf, size_t count, uint64_t offset)
Definition: fec_read.cpp:497
char data[Size]
Definition: incremental_server.cpp:1
Definition: fec_private.h:131
bool valid
Definition: fec_private.h:132
std::vector< uint8_t > vbmeta
Definition: fec_private.h:133
hashtree_info hashtree
Definition: fec_private.h:134
Definition: fec_private.h:72
uint64_t start
Definition: fec_private.h:79
uint32_t size
Definition: fec_private.h:76
uint64_t rounds
Definition: fec_private.h:78
uint64_t blocks
Definition: fec_private.h:77
int rsn
Definition: fec_private.h:75
bool valid
Definition: fec_private.h:73
int roots
Definition: fec_private.h:74
Definition: fec_private.h:137
int mode
Definition: fec_private.h:141
int flags
Definition: fec_private.h:140
ecc_info ecc
Definition: fec_private.h:138
verity_info verity
Definition: fec_private.h:147
avb_info avb
Definition: fec_private.h:148
uint64_t pos
Definition: fec_private.h:144
hashtree_info hashtree() const
Definition: fec_private.h:150
int fd
Definition: fec_private.h:139
uint64_t errors
Definition: fec_private.h:142
uint64_t data_size
Definition: fec_private.h:143
uint64_t size
Definition: fec_private.h:145
Definition: fec_private.h:82
bool ecc_read_hashes(fec_handle *f, uint64_t hash_offset, uint8_t *hash, uint64_t data_offset, uint8_t *data)
Definition: fec_verity.cpp:181
uint64_t data_blocks
Definition: fec_private.h:84
int get_hash(const uint8_t *block, uint8_t *hash)
Definition: fec_verity.cpp:124
int initialize(uint64_t hash_start, uint64_t data_blocks, const std::vector< uint8_t > &salt, int nid)
Definition: fec_verity.cpp:141
std::vector< uint8_t > salt
Definition: fec_private.h:90
uint32_t padded_digest_length_
Definition: fec_private.h:119
uint64_t hash_start
Definition: fec_private.h:86
int nid_
Definition: fec_private.h:117
std::vector< uint8_t > zero_hash
Definition: fec_private.h:91
uint32_t digest_length_
Definition: fec_private.h:118
std::vector< uint8_t > hash_data
Definition: fec_private.h:89
int verify_tree(const fec_handle *f, const uint8_t *root)
Definition: fec_verity.cpp:204
bool check_block_hash_with_index(uint64_t index, const uint8_t *block)
Definition: fec_verity.cpp:171
bool check_block_hash(const uint8_t *expected, const uint8_t *block)
Definition: fec_verity.cpp:157
Definition: fec_private.h:64
uint32_t version
Definition: fec_private.h:66
uint32_t length
Definition: fec_private.h:68
uint32_t magic
Definition: fec_private.h:65
uint8_t signature[ANDROID_PUBKEY_MODULUS_SIZE]
Definition: fec_private.h:67
Definition: fec_private.h:122
verity_header ecc_header
Definition: fec_private.h:128
bool disabled
Definition: fec_private.h:123
std::string table
Definition: fec_private.h:124
hashtree_info hashtree
Definition: fec_private.h:126
verity_header header
Definition: fec_private.h:127
uint64_t metadata_start
Definition: fec_private.h:125