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