2 * dm-verity volume handling
4 * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #define VERITY_MAX_LEVELS 63
31 static unsigned get_bits_up(size_t u)
39 static unsigned get_bits_down(size_t u)
47 static int verify_zero(struct crypt_device *cd, FILE *wr, size_t bytes)
52 if (fread(block, bytes, 1, wr) != 1)
54 for (i = 0; i < bytes; i++)
56 log_err(cd, _("Spare area is not zeroed at position %" PRIu64 ".\n"),
63 static int verify_hash_block(const char *hash_name, int version,
64 char *hash, size_t hash_size,
65 const char *data, size_t data_size,
66 const char *salt, size_t salt_size)
68 struct crypt_hash *ctx = NULL;
71 if (crypt_hash_init(&ctx, hash_name))
74 if (version == 1 && (r = crypt_hash_write(ctx, salt, salt_size)))
77 if ((r = crypt_hash_write(ctx, data, data_size)))
80 if (version == 0 && (r = crypt_hash_write(ctx, salt, salt_size)))
83 r = crypt_hash_final(ctx, hash, hash_size);
85 crypt_hash_destroy(ctx);
89 static int create_or_verify(struct crypt_device *cd, FILE *rd, FILE *wr,
90 off_t data_block, size_t data_block_size,
91 off_t hash_block, size_t hash_block_size,
92 off_t blocks, int version,
93 const char *hash_name, int verify,
94 char *calculated_digest, size_t digest_size,
95 const char *salt, size_t salt_size)
97 char left_block[hash_block_size];
98 char data_buffer[data_block_size];
99 char read_digest[digest_size];
100 size_t hash_per_block = 1 << get_bits_down(hash_block_size / digest_size);
101 size_t digest_size_full = 1 << get_bits_up(digest_size);
102 off_t blocks_to_write = (blocks + hash_per_block - 1) / hash_per_block;
106 if (fseeko(rd, data_block * data_block_size, SEEK_SET))
109 if (wr && fseeko(wr, hash_block * hash_block_size, SEEK_SET))
112 memset(left_block, 0, hash_block_size);
113 while (blocks_to_write--) {
114 left_bytes = hash_block_size;
115 for (i = 0; i < hash_per_block; i++) {
119 if (fread(data_buffer, data_block_size, 1, rd) != 1)
122 if (verify_hash_block(hash_name, version,
123 calculated_digest, digest_size,
124 data_buffer, data_block_size,
131 if (fread(read_digest, digest_size, 1, wr) != 1)
133 if (memcmp(read_digest, calculated_digest, digest_size)) {
134 log_err(cd, _("Verification failed at position %" PRIu64 ".\n"),
135 ftello(rd) - data_block_size);
139 if (fwrite(calculated_digest, digest_size, 1, wr) != 1)
143 left_bytes -= digest_size;
145 if (digest_size_full - digest_size) {
147 r = verify_zero(cd, wr, digest_size_full - digest_size);
150 } else if (fwrite(left_block, digest_size_full - digest_size, 1, wr) != 1)
153 left_bytes -= digest_size_full;
156 if (wr && left_bytes) {
158 r = verify_zero(cd , wr, left_bytes);
161 } else if (fwrite(left_block, left_bytes, 1, wr) != 1)
169 static int VERITY_create_or_verify_hash(struct crypt_device *cd,
172 const char *hash_name,
173 const char *hash_device,
174 const char *data_device,
175 size_t hash_block_size,
176 size_t data_block_size,
184 char calculated_digest[digest_size];
185 FILE *data_file = NULL;
186 FILE *hash_file = NULL, *hash_file_2;
187 off_t hash_level_block[VERITY_MAX_LEVELS];
188 off_t hash_level_size[VERITY_MAX_LEVELS];
189 off_t data_file_blocks, s;
190 size_t hash_per_block, hash_per_block_bits;
191 uint64_t data_device_size;
194 log_dbg("Hash %s %s, data device %s, data blocks %" PRIu64
195 ", hash_device %s, offset %" PRIu64 ".",
196 verify ? "verification" : "creation", hash_name,
197 data_device, data_blocks, hash_device, hash_position);
200 r = device_size(data_device, &data_device_size);
204 data_file_blocks = data_device_size / data_block_size;
206 data_file_blocks = data_blocks;
208 hash_per_block_bits = get_bits_down(hash_block_size / digest_size);
209 hash_per_block = 1 << hash_per_block_bits;
210 if (!hash_per_block_bits)
214 if (data_file_blocks) {
215 while (hash_per_block_bits * levels < 64 &&
216 (data_file_blocks - 1) >> (hash_per_block_bits * levels))
220 if (levels > VERITY_MAX_LEVELS) {
221 log_err(cd, _("Too many tree levels for verity volume.\n"));
225 for (i = levels - 1; i >= 0; i--) {
226 hash_level_block[i] = hash_position;
227 // verity position of block data_file_blocks at level i
228 s = data_file_blocks >> (i * hash_per_block_bits);
229 s = (s + hash_per_block - 1) / hash_per_block;
230 hash_level_size[i] = s;
231 if (hash_position + s < hash_position ||
232 (hash_position + s) < 0 ||
233 (hash_position + s) != hash_position + s) {
234 log_dbg("Hash device offset overflow.");
240 data_file = fopen(data_device, "r");
242 log_err(cd, _("Cannot open device %s.\n"), data_device);
247 hash_file = fopen(hash_device, verify ? "r" : "r+");
249 log_err(cd, _("Cannot open device %s.\n"), hash_device);
254 memset(calculated_digest, 0, digest_size);
256 for (i = 0; i < levels; i++) {
258 r = create_or_verify(cd, data_file, hash_file,
260 hash_level_block[i], hash_block_size,
261 data_file_blocks, version, hash_name, verify,
262 calculated_digest, digest_size, salt, salt_size);
266 hash_file_2 = fopen(hash_device, "r");
271 r = create_or_verify(cd, hash_file_2, hash_file,
272 hash_level_block[i - 1], hash_block_size,
273 hash_level_block[i], hash_block_size,
274 hash_level_size[i - 1], version, hash_name, verify,
275 calculated_digest, digest_size, salt, salt_size);
283 r = create_or_verify(cd, hash_file, NULL,
284 hash_level_block[levels - 1], hash_block_size,
286 1, version, hash_name, verify,
287 calculated_digest, digest_size, salt, salt_size);
289 r = create_or_verify(cd, data_file, NULL,
292 data_file_blocks, version, hash_name, verify,
293 calculated_digest, digest_size, salt, salt_size);
296 log_err(cd, _("Verification of data area failed.\n"));
299 log_dbg("Verification of data area succeeded.");
301 /* root hash verification */
303 r = memcmp(root_hash, calculated_digest, digest_size) ? -EPERM : 0;
305 log_err(cd, _("Verification of root hash failed.\n"));
307 log_dbg("Verification of root hash succeeded.");
309 fsync(fileno(hash_file));
310 memcpy(root_hash, calculated_digest, digest_size);
320 /* Verify verity device using userspace crypto backend */
321 int VERITY_verify(struct crypt_device *cd,
322 struct crypt_params_verity *verity_hdr,
323 const char *data_device,
324 const char *hash_device,
325 const char *root_hash,
326 size_t root_hash_size)
328 return VERITY_create_or_verify_hash(cd, 1,
329 verity_hdr->hash_type,
330 verity_hdr->hash_name,
333 verity_hdr->hash_block_size,
334 verity_hdr->data_block_size,
335 verity_hdr->data_size,
336 VERITY_hash_offset_block(verity_hdr),
337 CONST_CAST(char*)root_hash,
340 verity_hdr->salt_size);
343 /* Create verity hash */
344 int VERITY_create(struct crypt_device *cd,
345 struct crypt_params_verity *verity_hdr,
346 const char *data_device,
347 const char *hash_device,
349 size_t root_hash_size)
351 int pgsize = crypt_getpagesize();
353 if (verity_hdr->salt_size > 256)
356 if (verity_hdr->hash_block_size > pgsize ||
357 verity_hdr->data_block_size > pgsize)
358 log_err(cd, _("WARNING: Kernel cannot activate device if block "
359 "size exceeds page size (%u).\n"), pgsize);
361 return VERITY_create_or_verify_hash(cd, 0,
362 verity_hdr->hash_type,
363 verity_hdr->hash_name,
366 verity_hdr->hash_block_size,
367 verity_hdr->data_block_size,
368 verity_hdr->data_size,
369 VERITY_hash_offset_block(verity_hdr),
373 verity_hdr->salt_size);