xref: /linux/drivers/md/dm-verity.h (revision 9557b4376d02088a33e5f4116bcc324d35a3b64c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
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
9  */
10 
11 #ifndef DM_VERITY_H
12 #define DM_VERITY_H
13 
14 #include <linux/dm-io.h>
15 #include <linux/dm-bufio.h>
16 #include <linux/device-mapper.h>
17 #include <linux/interrupt.h>
18 #include <crypto/hash.h>
19 
20 #define DM_VERITY_MAX_LEVELS		63
21 
22 enum verity_mode {
23 	DM_VERITY_MODE_EIO,
24 	DM_VERITY_MODE_LOGGING,
25 	DM_VERITY_MODE_RESTART,
26 	DM_VERITY_MODE_PANIC
27 };
28 
29 enum verity_block_type {
30 	DM_VERITY_BLOCK_TYPE_DATA,
31 	DM_VERITY_BLOCK_TYPE_METADATA
32 };
33 
34 struct dm_verity_fec;
35 
36 struct dm_verity {
37 	struct dm_dev *data_dev;
38 	struct dm_dev *hash_dev;
39 	struct dm_target *ti;
40 	struct dm_bufio_client *bufio;
41 	char *alg_name;
42 	struct crypto_ahash *ahash_tfm; /* either this or shash_tfm is set */
43 	struct crypto_shash *shash_tfm; /* either this or ahash_tfm is set */
44 	u8 *root_digest;	/* digest of the root block */
45 	u8 *salt;		/* salt: its size is salt_size */
46 	u8 *initial_hashstate;	/* salted initial state, if shash_tfm is set */
47 	u8 *zero_digest;	/* digest for a zero block */
48 	unsigned int salt_size;
49 	sector_t data_start;	/* data offset in 512-byte sectors */
50 	sector_t hash_start;	/* hash start in blocks */
51 	sector_t data_blocks;	/* the number of data blocks */
52 	sector_t hash_blocks;	/* the number of hash blocks */
53 	unsigned char data_dev_block_bits;	/* log2(data blocksize) */
54 	unsigned char hash_dev_block_bits;	/* log2(hash blocksize) */
55 	unsigned char hash_per_block_bits;	/* log2(hashes in hash block) */
56 	unsigned char levels;	/* the number of tree levels */
57 	unsigned char version;
58 	bool hash_failed:1;	/* set if hash of any block failed */
59 	bool use_bh_wq:1;	/* try to verify in BH wq before normal work-queue */
60 	unsigned int digest_size;	/* digest size for the current hash algorithm */
61 	unsigned int hash_reqsize; /* the size of temporary space for crypto */
62 	enum verity_mode mode;	/* mode for handling verification errors */
63 	unsigned int corrupted_errs;/* Number of errors for corrupted blocks */
64 
65 	struct workqueue_struct *verify_wq;
66 
67 	/* starting blocks for each tree level. 0 is the lowest level. */
68 	sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
69 
70 	struct dm_verity_fec *fec;	/* forward error correction */
71 	unsigned long *validated_blocks; /* bitset blocks validated */
72 
73 	char *signature_key_desc; /* signature keyring reference */
74 
75 	struct dm_io_client *io;
76 	mempool_t recheck_pool;
77 };
78 
79 struct dm_verity_io {
80 	struct dm_verity *v;
81 
82 	/* original value of bio->bi_end_io */
83 	bio_end_io_t *orig_bi_end_io;
84 
85 	struct bvec_iter iter;
86 
87 	sector_t block;
88 	unsigned int n_blocks;
89 	bool in_bh;
90 
91 	struct work_struct work;
92 	struct work_struct bh_work;
93 
94 	u8 real_digest[HASH_MAX_DIGESTSIZE];
95 	u8 want_digest[HASH_MAX_DIGESTSIZE];
96 
97 	/*
98 	 * This struct is followed by a variable-sized hash request of size
99 	 * v->hash_reqsize, either a struct ahash_request or a struct shash_desc
100 	 * (depending on whether ahash_tfm or shash_tfm is being used).  To
101 	 * access it, use verity_io_hash_req().
102 	 */
103 };
104 
105 static inline void *verity_io_hash_req(struct dm_verity *v,
106 				       struct dm_verity_io *io)
107 {
108 	return io + 1;
109 }
110 
111 static inline u8 *verity_io_real_digest(struct dm_verity *v,
112 					struct dm_verity_io *io)
113 {
114 	return io->real_digest;
115 }
116 
117 static inline u8 *verity_io_want_digest(struct dm_verity *v,
118 					struct dm_verity_io *io)
119 {
120 	return io->want_digest;
121 }
122 
123 extern int verity_hash(struct dm_verity *v, struct dm_verity_io *io,
124 		       const u8 *data, size_t len, u8 *digest, bool may_sleep);
125 
126 extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
127 				 sector_t block, u8 *digest, bool *is_zero);
128 
129 extern bool dm_is_verity_target(struct dm_target *ti);
130 extern int dm_verity_get_mode(struct dm_target *ti);
131 extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest,
132 				     unsigned int *digest_size);
133 
134 #endif /* DM_VERITY_H */
135