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