c40bccf2da2ac70cd414f4ba9aeef628858eb4c4
[platform/upstream/cryptsetup.git] / lib / verity / verity_hash.c
1 /*
2  * dm-verity volume handling
3  *
4  * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
5  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25
26 #include "verity.h"
27 #include "internal.h"
28
29 #define VERITY_MAX_LEVELS       63
30
31 static unsigned get_bits_up(size_t u)
32 {
33         unsigned i = 0;
34         while ((1U << i) < u)
35                 i++;
36         return i;
37 }
38
39 static unsigned get_bits_down(size_t u)
40 {
41         unsigned i = 0;
42         while ((u >> i) > 1U)
43                 i++;
44         return i;
45 }
46
47 static int verify_zero(struct crypt_device *cd, FILE *wr, size_t bytes)
48 {
49         char block[bytes];
50         size_t i;
51
52         if (fread(block, bytes, 1, wr) != 1) {
53                 log_dbg("EIO while reading spare area.");
54                 return -EIO;
55         }
56         for (i = 0; i < bytes; i++)
57                 if (block[i]) {
58                         log_err(cd, _("Spare area is not zeroed at position %" PRIu64 ".\n"),
59                                 ftello(wr) - bytes);
60                         return -EPERM;
61                 }
62         return 0;
63 }
64
65 static int verify_hash_block(const char *hash_name, int version,
66                               char *hash, size_t hash_size,
67                               const char *data, size_t data_size,
68                               const char *salt, size_t salt_size)
69 {
70         struct crypt_hash *ctx = NULL;
71         int r;
72
73         if (crypt_hash_init(&ctx, hash_name))
74                 return -EINVAL;
75
76         if (version == 1 && (r = crypt_hash_write(ctx, salt, salt_size)))
77                 goto out;
78
79         if ((r = crypt_hash_write(ctx, data, data_size)))
80                 goto out;
81
82         if (version == 0 && (r = crypt_hash_write(ctx, salt, salt_size)))
83                 goto out;
84
85         r = crypt_hash_final(ctx, hash, hash_size);
86 out:
87         crypt_hash_destroy(ctx);
88         return r;
89 }
90
91 static int create_or_verify(struct crypt_device *cd, FILE *rd, FILE *wr,
92                                    off_t data_block, size_t data_block_size,
93                                    off_t hash_block, size_t hash_block_size,
94                                    off_t blocks, int version,
95                                    const char *hash_name, int verify,
96                                    char *calculated_digest, size_t digest_size,
97                                    const char *salt, size_t salt_size)
98 {
99         char left_block[hash_block_size];
100         char data_buffer[data_block_size];
101         char read_digest[digest_size];
102         size_t hash_per_block = 1 << get_bits_down(hash_block_size / digest_size);
103         size_t digest_size_full = 1 << get_bits_up(digest_size);
104         off_t blocks_to_write = (blocks + hash_per_block - 1) / hash_per_block;
105         size_t left_bytes;
106         unsigned i;
107         int r;
108
109         if (fseeko(rd, data_block * data_block_size, SEEK_SET)) {
110                 log_dbg("Cannot seek to requested position in data device.");
111                 return -EIO;
112         }
113
114         if (wr && fseeko(wr, hash_block * hash_block_size, SEEK_SET)) {
115                 log_dbg("Cannot seek to requested position in hash device.");
116                 return -EIO;
117         }
118
119         memset(left_block, 0, hash_block_size);
120         while (blocks_to_write--) {
121                 left_bytes = hash_block_size;
122                 for (i = 0; i < hash_per_block; i++) {
123                         if (!blocks)
124                                 break;
125                         blocks--;
126                         if (fread(data_buffer, data_block_size, 1, rd) != 1) {
127                                 log_dbg("Cannot read data device block.");
128                                 return -EIO;
129                         }
130
131                         if (verify_hash_block(hash_name, version,
132                                         calculated_digest, digest_size,
133                                         data_buffer, data_block_size,
134                                         salt, salt_size))
135                                 return -EINVAL;
136
137                         if (!wr)
138                                 break;
139                         if (verify) {
140                                 if (fread(read_digest, digest_size, 1, wr) != 1) {
141                                         log_dbg("Cannot read digest form hash device.");
142                                         return -EIO;
143                                 }
144                                 if (memcmp(read_digest, calculated_digest, digest_size)) {
145                                         log_err(cd, _("Verification failed at position %" PRIu64 ".\n"),
146                                                 ftello(rd) - data_block_size);
147                                         return -EPERM;
148                                 }
149                         } else {
150                                 if (fwrite(calculated_digest, digest_size, 1, wr) != 1) {
151                                         log_dbg("Cannot write digest to hash device.");
152                                         return -EIO;
153                                 }
154                         }
155                         if (version == 0) {
156                                 left_bytes -= digest_size;
157                         } else {
158                                 if (digest_size_full - digest_size) {
159                                         if (verify) {
160                                                 r = verify_zero(cd, wr, digest_size_full - digest_size);
161                                                 if (r)
162                                                         return r;
163                                         } else if (fwrite(left_block, digest_size_full - digest_size, 1, wr) != 1) {
164                                                 log_dbg("Cannot write spare area to hash device.");
165                                                 return -EIO;
166                                         }
167                                 }
168                                 left_bytes -= digest_size_full;
169                         }
170                 }
171                 if (wr && left_bytes) {
172                         if (verify) {
173                                 r = verify_zero(cd , wr, left_bytes);
174                                 if (r)
175                                         return r;
176                         } else if (fwrite(left_block, left_bytes, 1, wr) != 1) {
177                                 log_dbg("Cannot write remaining spare area to hash device.");
178                                 return -EIO;
179                         }
180                 }
181         }
182
183         return 0;
184 }
185
186 static int VERITY_create_or_verify_hash(struct crypt_device *cd,
187         int verify,
188         int version,
189         const char *hash_name,
190         const char *hash_device,
191         const char *data_device,
192         size_t hash_block_size,
193         size_t data_block_size,
194         off_t data_blocks,
195         off_t hash_position,
196         char *root_hash,
197         size_t digest_size,
198         const char *salt,
199         size_t salt_size)
200 {
201         char calculated_digest[digest_size];
202         FILE *data_file = NULL;
203         FILE *hash_file = NULL, *hash_file_2;
204         off_t hash_level_block[VERITY_MAX_LEVELS];
205         off_t hash_level_size[VERITY_MAX_LEVELS];
206         off_t data_file_blocks, s;
207         size_t hash_per_block, hash_per_block_bits;
208         uint64_t data_device_size = 0, hash_device_size = 0;
209         int levels, i, r;
210
211         log_dbg("Hash %s %s, data device %s, data blocks %" PRIu64
212                 ", hash_device %s, offset %" PRIu64 ".",
213                 verify ? "verification" : "creation", hash_name,
214                 data_device, data_blocks, hash_device, hash_position);
215
216         if (!data_blocks) {
217                 r = device_size(data_device, &data_device_size);
218                 if (r < 0)
219                         return r;
220
221                 data_file_blocks = data_device_size / data_block_size;
222         } else {
223                 data_device_size = data_blocks * data_block_size;
224                 data_file_blocks = data_blocks;
225         }
226
227         hash_per_block_bits = get_bits_down(hash_block_size / digest_size);
228         hash_per_block = 1 << hash_per_block_bits;
229         if (!hash_per_block_bits)
230                 return -EINVAL;
231
232         levels = 0;
233         if (data_file_blocks) {
234                 while (hash_per_block_bits * levels < 64 &&
235                        (data_file_blocks - 1) >> (hash_per_block_bits * levels))
236                         levels++;
237         }
238         log_dbg("Using %d hash levels.", levels);
239
240         if (levels > VERITY_MAX_LEVELS) {
241                 log_err(cd, _("Too many tree levels for verity volume.\n"));
242                 return -EINVAL;
243         }
244
245         for (i = levels - 1; i >= 0; i--) {
246                 hash_level_block[i] = hash_position;
247                 // verity position of block data_file_blocks at level i
248                 s = data_file_blocks >> (i * hash_per_block_bits);
249                 s = (s + hash_per_block - 1) / hash_per_block;
250                 hash_level_size[i] = s;
251                 if (hash_position + s < hash_position ||
252                     (hash_position + s) < 0 ||
253                     (hash_position + s) != hash_position + s) {
254                         log_dbg("Hash device offset overflow.");
255                         return -EINVAL;
256                 }
257                 hash_position += s;
258         }
259         hash_device_size = hash_position * hash_block_size;
260
261         log_dbg("Data device size required: %" PRIu64 " bytes.",
262                 data_device_size);
263         data_file = fopen(data_device, "r");
264         if (!data_file) {
265                 log_err(cd, _("Cannot open device %s.\n"), data_device);
266                 r = -EIO;
267                 goto out;
268         }
269
270         log_dbg("Hash device size required: %" PRIu64 " bytes.",
271                 hash_device_size);
272         hash_file = fopen(hash_device, verify ? "r" : "r+");
273         if (!hash_file) {
274                 log_err(cd, _("Cannot open device %s.\n"), hash_device);
275                 r = -EIO;
276                 goto out;
277         }
278
279         memset(calculated_digest, 0, digest_size);
280
281         for (i = 0; i < levels; i++) {
282                 if (!i) {
283                         r = create_or_verify(cd, data_file, hash_file,
284                                                     0, data_block_size,
285                                                     hash_level_block[i], hash_block_size,
286                                                     data_file_blocks, version, hash_name, verify,
287                                                     calculated_digest, digest_size, salt, salt_size);
288                         if (r)
289                                 goto out;
290                 } else {
291                         hash_file_2 = fopen(hash_device, "r");
292                         if (!hash_file_2) {
293                                 log_err(cd, _("Cannot open device %s.\n"), hash_device);
294                                 r = -EIO;
295                                 goto out;
296                         }
297                         r = create_or_verify(cd, hash_file_2, hash_file,
298                                                     hash_level_block[i - 1], hash_block_size,
299                                                     hash_level_block[i], hash_block_size,
300                                                     hash_level_size[i - 1], version, hash_name, verify,
301                                                     calculated_digest, digest_size, salt, salt_size);
302                         fclose(hash_file_2);
303                         if (r)
304                                 goto out;
305                 }
306         }
307
308         if (levels)
309                 r = create_or_verify(cd, hash_file, NULL,
310                                             hash_level_block[levels - 1], hash_block_size,
311                                             0, hash_block_size,
312                                             1, version, hash_name, verify,
313                                             calculated_digest, digest_size, salt, salt_size);
314         else
315                 r = create_or_verify(cd, data_file, NULL,
316                                             0, data_block_size,
317                                             0, hash_block_size,
318                                             data_file_blocks, version, hash_name, verify,
319                                             calculated_digest, digest_size, salt, salt_size);
320 out:
321         if (verify) {
322                 if (r)
323                         log_err(cd, _("Verification of data area failed.\n"));
324                 else {
325                         log_dbg("Verification of data area succeeded.");
326                         r = memcmp(root_hash, calculated_digest, digest_size) ? -EPERM : 0;
327                         if (r)
328                                 log_err(cd, _("Verification of root hash failed.\n"));
329                         else
330                                 log_dbg("Verification of root hash succeeded.");
331                 }
332         } else {
333                 if (r == -EIO)
334                         log_err(cd, _("Input/output error while creating hash area.\n"));
335                 else if (r)
336                         log_err(cd, _("Creation of hash area failed.\n"));
337                 else {
338                         fsync(fileno(hash_file));
339                         memcpy(root_hash, calculated_digest, digest_size);
340                 }
341         }
342
343         if (data_file)
344                 fclose(data_file);
345         if (hash_file)
346                 fclose(hash_file);
347         return r;
348 }
349
350 /* Verify verity device using userspace crypto backend */
351 int VERITY_verify(struct crypt_device *cd,
352                   struct crypt_params_verity *verity_hdr,
353                   const char *data_device,
354                   const char *hash_device,
355                   const char *root_hash,
356                   size_t root_hash_size)
357 {
358         return VERITY_create_or_verify_hash(cd, 1,
359                 verity_hdr->hash_type,
360                 verity_hdr->hash_name,
361                 hash_device,
362                 data_device,
363                 verity_hdr->hash_block_size,
364                 verity_hdr->data_block_size,
365                 verity_hdr->data_size,
366                 VERITY_hash_offset_block(verity_hdr),
367                 CONST_CAST(char*)root_hash,
368                 root_hash_size,
369                 verity_hdr->salt,
370                 verity_hdr->salt_size);
371 }
372
373 /* Create verity hash */
374 int VERITY_create(struct crypt_device *cd,
375                   struct crypt_params_verity *verity_hdr,
376                   const char *data_device,
377                   const char *hash_device,
378                   char *root_hash,
379                   size_t root_hash_size)
380 {
381         unsigned pgsize = crypt_getpagesize();
382
383         if (verity_hdr->salt_size > 256)
384                 return -EINVAL;
385
386         if (verity_hdr->hash_block_size > pgsize ||
387             verity_hdr->data_block_size > pgsize)
388                 log_err(cd, _("WARNING: Kernel cannot activate device if block "
389                               "size exceeds page size (%u).\n"), pgsize);
390
391         return VERITY_create_or_verify_hash(cd, 0,
392                 verity_hdr->hash_type,
393                 verity_hdr->hash_name,
394                 hash_device,
395                 data_device,
396                 verity_hdr->hash_block_size,
397                 verity_hdr->data_block_size,
398                 verity_hdr->data_size,
399                 VERITY_hash_offset_block(verity_hdr),
400                 root_hash,
401                 root_hash_size,
402                 verity_hdr->salt,
403                 verity_hdr->salt_size);
404 }