1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 Red Hat, Inc.
4 * Copyright (C) 2015 Google, Inc.
6 * Author: Mikulas Patocka <mpatocka@redhat.com>
8 * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
14 #include <linux/dm-bufio.h>
15 #include <linux/device-mapper.h>
16 #include <linux/interrupt.h>
17 #include <crypto/hash.h>
19 #define DM_VERITY_MAX_LEVELS 63
23 DM_VERITY_MODE_LOGGING,
24 DM_VERITY_MODE_RESTART,
28 enum verity_block_type {
29 DM_VERITY_BLOCK_TYPE_DATA,
30 DM_VERITY_BLOCK_TYPE_METADATA
36 struct dm_dev *data_dev;
37 struct dm_dev *hash_dev;
39 struct dm_bufio_client *bufio;
41 struct crypto_ahash *tfm;
42 u8 *root_digest; /* digest of the root block */
43 u8 *salt; /* salt: its size is salt_size */
44 u8 *zero_digest; /* digest for a zero block */
45 unsigned int salt_size;
46 sector_t data_start; /* data offset in 512-byte sectors */
47 sector_t hash_start; /* hash start in blocks */
48 sector_t data_blocks; /* the number of data blocks */
49 sector_t hash_blocks; /* the number of hash blocks */
50 unsigned char data_dev_block_bits; /* log2(data blocksize) */
51 unsigned char hash_dev_block_bits; /* log2(hash blocksize) */
52 unsigned char hash_per_block_bits; /* log2(hashes in hash block) */
53 unsigned char levels; /* the number of tree levels */
54 unsigned char version;
55 bool hash_failed:1; /* set if hash of any block failed */
56 bool use_tasklet:1; /* try to verify in tasklet before work-queue */
57 unsigned int digest_size; /* digest size for the current hash algorithm */
58 unsigned int ahash_reqsize;/* the size of temporary space for crypto */
59 enum verity_mode mode; /* mode for handling verification errors */
60 unsigned int corrupted_errs;/* Number of errors for corrupted blocks */
62 struct workqueue_struct *verify_wq;
64 /* starting blocks for each tree level. 0 is the lowest level. */
65 sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
67 struct dm_verity_fec *fec; /* forward error correction */
68 unsigned long *validated_blocks; /* bitset blocks validated */
70 char *signature_key_desc; /* signature keyring reference */
76 /* original value of bio->bi_end_io */
77 bio_end_io_t *orig_bi_end_io;
80 unsigned int n_blocks;
83 struct bvec_iter iter;
85 struct work_struct work;
86 struct tasklet_struct tasklet;
89 * Three variably-size fields follow this struct:
91 * u8 hash_req[v->ahash_reqsize];
92 * u8 real_digest[v->digest_size];
93 * u8 want_digest[v->digest_size];
95 * To access them use: verity_io_hash_req(), verity_io_real_digest()
96 * and verity_io_want_digest().
100 static inline struct ahash_request *verity_io_hash_req(struct dm_verity *v,
101 struct dm_verity_io *io)
103 return (struct ahash_request *)(io + 1);
106 static inline u8 *verity_io_real_digest(struct dm_verity *v,
107 struct dm_verity_io *io)
109 return (u8 *)(io + 1) + v->ahash_reqsize;
112 static inline u8 *verity_io_want_digest(struct dm_verity *v,
113 struct dm_verity_io *io)
115 return (u8 *)(io + 1) + v->ahash_reqsize + v->digest_size;
118 extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
119 struct bvec_iter *iter,
120 int (*process)(struct dm_verity *v,
121 struct dm_verity_io *io,
122 u8 *data, size_t len));
124 extern int verity_hash(struct dm_verity *v, struct ahash_request *req,
125 const u8 *data, size_t len, u8 *digest, bool may_sleep);
127 extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
128 sector_t block, u8 *digest, bool *is_zero);
130 extern bool dm_is_verity_target(struct dm_target *ti);
131 extern int dm_verity_get_mode(struct dm_target *ti);
132 extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest,
133 unsigned int *digest_size);
135 #endif /* DM_VERITY_H */