xref: /linux/drivers/md/dm-verity.h (revision d358e5254674b70f34c847715ca509e46eb81e6f)
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 #include <crypto/sha2.h>
20 
21 #define DM_VERITY_MAX_LEVELS		63
22 
23 enum verity_mode {
24 	DM_VERITY_MODE_EIO,
25 	DM_VERITY_MODE_LOGGING,
26 	DM_VERITY_MODE_RESTART,
27 	DM_VERITY_MODE_PANIC
28 };
29 
30 enum verity_block_type {
31 	DM_VERITY_BLOCK_TYPE_DATA,
32 	DM_VERITY_BLOCK_TYPE_METADATA
33 };
34 
35 struct dm_verity_fec;
36 
37 struct dm_verity {
38 	struct dm_dev *data_dev;
39 	struct dm_dev *hash_dev;
40 	struct dm_target *ti;
41 	struct dm_bufio_client *bufio;
42 	char *alg_name;
43 	struct crypto_shash *shash_tfm;
44 	u8 *root_digest;	/* digest of the root block */
45 	u8 *salt;		/* salt: its size is salt_size */
46 	union {
47 		struct sha256_ctx *sha256;	/* for use_sha256_lib=1 */
48 		u8 *shash;			/* for use_sha256_lib=0 */
49 	} initial_hashstate; /* salted initial state, if version >= 1 */
50 	u8 *zero_digest;	/* digest for a zero block */
51 #ifdef CONFIG_SECURITY
52 	u8 *root_digest_sig;	/* signature of the root digest */
53 	unsigned int sig_size;	/* root digest signature size */
54 #endif /* CONFIG_SECURITY */
55 	unsigned int salt_size;
56 	sector_t hash_start;	/* hash start in blocks */
57 	sector_t data_blocks;	/* the number of data blocks */
58 	sector_t hash_blocks;	/* the number of hash blocks */
59 	unsigned char data_dev_block_bits;	/* log2(data blocksize) */
60 	unsigned char hash_dev_block_bits;	/* log2(hash blocksize) */
61 	unsigned char hash_per_block_bits;	/* log2(hashes in hash block) */
62 	unsigned char levels;	/* the number of tree levels */
63 	unsigned char version;
64 	bool hash_failed:1;	/* set if hash of any block failed */
65 	bool use_bh_wq:1;	/* try to verify in BH wq before normal work-queue */
66 	bool use_sha256_lib:1;	/* use SHA-256 library instead of generic crypto API */
67 	bool use_sha256_finup_2x:1; /* use interleaved hashing optimization */
68 	unsigned int digest_size;	/* digest size for the current hash algorithm */
69 	enum verity_mode mode;	/* mode for handling verification errors */
70 	enum verity_mode error_mode;/* mode for handling I/O errors */
71 	unsigned int corrupted_errs;/* Number of errors for corrupted blocks */
72 
73 	struct workqueue_struct *verify_wq;
74 
75 	/* starting blocks for each tree level. 0 is the lowest level. */
76 	sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
77 
78 	struct dm_verity_fec *fec;	/* forward error correction */
79 	unsigned long *validated_blocks; /* bitset blocks validated */
80 
81 	char *signature_key_desc; /* signature keyring reference */
82 
83 	struct dm_io_client *io;
84 	mempool_t recheck_pool;
85 };
86 
87 struct pending_block {
88 	void *data;
89 	sector_t blkno;
90 	u8 want_digest[HASH_MAX_DIGESTSIZE];
91 	u8 real_digest[HASH_MAX_DIGESTSIZE];
92 };
93 
94 struct dm_verity_io {
95 	struct dm_verity *v;
96 
97 	/* original value of bio->bi_end_io */
98 	bio_end_io_t *orig_bi_end_io;
99 
100 	struct bvec_iter iter;
101 
102 	sector_t block;
103 	unsigned int n_blocks;
104 	bool in_bh;
105 	bool had_mismatch;
106 
107 	struct work_struct work;
108 	struct work_struct bh_work;
109 
110 	u8 tmp_digest[HASH_MAX_DIGESTSIZE];
111 
112 	/*
113 	 * This is the queue of data blocks that are pending verification.  When
114 	 * the crypto layer supports interleaved hashing, we allow multiple
115 	 * blocks to be queued up in order to utilize it.  This can improve
116 	 * performance significantly vs. sequential hashing of each block.
117 	 */
118 	int num_pending;
119 	struct pending_block pending_blocks[2];
120 
121 	/*
122 	 * Temporary space for hashing.  Either sha256 or shash is used,
123 	 * depending on the value of use_sha256_lib.  If shash is used,
124 	 * then this field is variable-length, with total size
125 	 * sizeof(struct shash_desc) + crypto_shash_descsize(shash_tfm).
126 	 * For this reason, this field must be the end of the struct.
127 	 */
128 	union {
129 		struct sha256_ctx sha256;
130 		struct shash_desc shash;
131 	} hash_ctx;
132 };
133 
134 extern int verity_hash(struct dm_verity *v, struct dm_verity_io *io,
135 		       const u8 *data, size_t len, u8 *digest);
136 
137 extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
138 				 sector_t block, u8 *digest, bool *is_zero);
139 
140 extern bool dm_is_verity_target(struct dm_target *ti);
141 extern int dm_verity_get_mode(struct dm_target *ti);
142 extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest,
143 				     unsigned int *digest_size);
144 
145 #endif /* DM_VERITY_H */
146