Commit | Line | Data |
---|---|---|
55716d26 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
ffa39380 ST |
2 | /* |
3 | * Copyright (C) 2012 Red Hat, Inc. | |
4 | * Copyright (C) 2015 Google, Inc. | |
5 | * | |
6 | * Author: Mikulas Patocka <mpatocka@redhat.com> | |
7 | * | |
8 | * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors | |
ffa39380 ST |
9 | */ |
10 | ||
11 | #ifndef DM_VERITY_H | |
12 | #define DM_VERITY_H | |
13 | ||
afa53df8 | 14 | #include <linux/dm-bufio.h> |
ffa39380 ST |
15 | #include <linux/device-mapper.h> |
16 | #include <crypto/hash.h> | |
17 | ||
18 | #define DM_VERITY_MAX_LEVELS 63 | |
19 | ||
20 | enum verity_mode { | |
21 | DM_VERITY_MODE_EIO, | |
22 | DM_VERITY_MODE_LOGGING, | |
23 | DM_VERITY_MODE_RESTART | |
24 | }; | |
25 | ||
26 | enum verity_block_type { | |
27 | DM_VERITY_BLOCK_TYPE_DATA, | |
28 | DM_VERITY_BLOCK_TYPE_METADATA | |
29 | }; | |
30 | ||
a739ff3f ST |
31 | struct dm_verity_fec; |
32 | ||
ffa39380 ST |
33 | struct dm_verity { |
34 | struct dm_dev *data_dev; | |
35 | struct dm_dev *hash_dev; | |
36 | struct dm_target *ti; | |
37 | struct dm_bufio_client *bufio; | |
38 | char *alg_name; | |
d1ac3ff0 | 39 | struct crypto_ahash *tfm; |
ffa39380 ST |
40 | u8 *root_digest; /* digest of the root block */ |
41 | u8 *salt; /* salt: its size is salt_size */ | |
0cc37c2d | 42 | u8 *zero_digest; /* digest for a zero block */ |
ffa39380 ST |
43 | unsigned salt_size; |
44 | sector_t data_start; /* data offset in 512-byte sectors */ | |
45 | sector_t hash_start; /* hash start in blocks */ | |
46 | sector_t data_blocks; /* the number of data blocks */ | |
47 | sector_t hash_blocks; /* the number of hash blocks */ | |
48 | unsigned char data_dev_block_bits; /* log2(data blocksize) */ | |
49 | unsigned char hash_dev_block_bits; /* log2(hash blocksize) */ | |
50 | unsigned char hash_per_block_bits; /* log2(hashes in hash block) */ | |
51 | unsigned char levels; /* the number of tree levels */ | |
52 | unsigned char version; | |
53 | unsigned digest_size; /* digest size for the current hash algorithm */ | |
d1ac3ff0 | 54 | unsigned int ahash_reqsize;/* the size of temporary space for crypto */ |
ffa39380 ST |
55 | int hash_failed; /* set to 1 if hash of any block failed */ |
56 | enum verity_mode mode; /* mode for handling verification errors */ | |
57 | unsigned corrupted_errs;/* Number of errors for corrupted blocks */ | |
58 | ||
59 | struct workqueue_struct *verify_wq; | |
60 | ||
61 | /* starting blocks for each tree level. 0 is the lowest level. */ | |
62 | sector_t hash_level_block[DM_VERITY_MAX_LEVELS]; | |
a739ff3f ST |
63 | |
64 | struct dm_verity_fec *fec; /* forward error correction */ | |
843f38d3 | 65 | unsigned long *validated_blocks; /* bitset blocks validated */ |
88cd3e6c JK |
66 | |
67 | char *signature_key_desc; /* signature keyring reference */ | |
ffa39380 ST |
68 | }; |
69 | ||
70 | struct dm_verity_io { | |
71 | struct dm_verity *v; | |
72 | ||
73 | /* original value of bio->bi_end_io */ | |
74 | bio_end_io_t *orig_bi_end_io; | |
75 | ||
76 | sector_t block; | |
77 | unsigned n_blocks; | |
78 | ||
79 | struct bvec_iter iter; | |
80 | ||
81 | struct work_struct work; | |
82 | ||
83 | /* | |
84 | * Three variably-size fields follow this struct: | |
85 | * | |
d1ac3ff0 | 86 | * u8 hash_req[v->ahash_reqsize]; |
ffa39380 ST |
87 | * u8 real_digest[v->digest_size]; |
88 | * u8 want_digest[v->digest_size]; | |
89 | * | |
d1ac3ff0 | 90 | * To access them use: verity_io_hash_req(), verity_io_real_digest() |
ffa39380 ST |
91 | * and verity_io_want_digest(). |
92 | */ | |
93 | }; | |
94 | ||
d1ac3ff0 | 95 | static inline struct ahash_request *verity_io_hash_req(struct dm_verity *v, |
ffa39380 ST |
96 | struct dm_verity_io *io) |
97 | { | |
d1ac3ff0 | 98 | return (struct ahash_request *)(io + 1); |
ffa39380 ST |
99 | } |
100 | ||
101 | static inline u8 *verity_io_real_digest(struct dm_verity *v, | |
102 | struct dm_verity_io *io) | |
103 | { | |
d1ac3ff0 | 104 | return (u8 *)(io + 1) + v->ahash_reqsize; |
ffa39380 ST |
105 | } |
106 | ||
107 | static inline u8 *verity_io_want_digest(struct dm_verity *v, | |
108 | struct dm_verity_io *io) | |
109 | { | |
d1ac3ff0 | 110 | return (u8 *)(io + 1) + v->ahash_reqsize + v->digest_size; |
ffa39380 ST |
111 | } |
112 | ||
a739ff3f ST |
113 | static inline u8 *verity_io_digest_end(struct dm_verity *v, |
114 | struct dm_verity_io *io) | |
115 | { | |
116 | return verity_io_want_digest(v, io) + v->digest_size; | |
117 | } | |
118 | ||
bb4d73ac ST |
119 | extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io, |
120 | struct bvec_iter *iter, | |
121 | int (*process)(struct dm_verity *v, | |
122 | struct dm_verity_io *io, | |
123 | u8 *data, size_t len)); | |
124 | ||
d1ac3ff0 | 125 | extern int verity_hash(struct dm_verity *v, struct ahash_request *req, |
ffa39380 ST |
126 | const u8 *data, size_t len, u8 *digest); |
127 | ||
128 | extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io, | |
0cc37c2d | 129 | sector_t block, u8 *digest, bool *is_zero); |
ffa39380 ST |
130 | |
131 | #endif /* DM_VERITY_H */ |