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