2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2015 Google, Inc.
5 * Author: Mikulas Patocka <mpatocka@redhat.com>
7 * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
9 * This file is released under the GPLv2.
16 #include <linux/device-mapper.h>
17 #include <crypto/hash.h>
19 #define DM_VERITY_MAX_LEVELS 63
23 DM_VERITY_MODE_LOGGING,
24 DM_VERITY_MODE_RESTART
27 enum verity_block_type {
28 DM_VERITY_BLOCK_TYPE_DATA,
29 DM_VERITY_BLOCK_TYPE_METADATA
35 struct dm_dev *data_dev;
36 struct dm_dev *hash_dev;
38 struct dm_bufio_client *bufio;
40 struct crypto_ahash *tfm;
41 u8 *root_digest; /* digest of the root block */
42 u8 *salt; /* salt: its size is salt_size */
43 u8 *zero_digest; /* digest for a zero block */
45 sector_t data_start; /* data offset in 512-byte sectors */
46 sector_t hash_start; /* hash start in blocks */
47 sector_t data_blocks; /* the number of data blocks */
48 sector_t hash_blocks; /* the number of hash blocks */
49 unsigned char data_dev_block_bits; /* log2(data blocksize) */
50 unsigned char hash_dev_block_bits; /* log2(hash blocksize) */
51 unsigned char hash_per_block_bits; /* log2(hashes in hash block) */
52 unsigned char levels; /* the number of tree levels */
53 unsigned char version;
54 unsigned digest_size; /* digest size for the current hash algorithm */
55 unsigned int ahash_reqsize;/* the size of temporary space for crypto */
56 int hash_failed; /* set to 1 if hash of any block failed */
57 enum verity_mode mode; /* mode for handling verification errors */
58 unsigned corrupted_errs;/* Number of errors for corrupted blocks */
60 struct workqueue_struct *verify_wq;
62 /* starting blocks for each tree level. 0 is the lowest level. */
63 sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
65 struct dm_verity_fec *fec; /* forward error correction */
71 /* original value of bio->bi_end_io */
72 bio_end_io_t *orig_bi_end_io;
77 struct bvec_iter iter;
79 struct work_struct work;
82 * Three variably-size fields follow this struct:
84 * u8 hash_req[v->ahash_reqsize];
85 * u8 real_digest[v->digest_size];
86 * u8 want_digest[v->digest_size];
88 * To access them use: verity_io_hash_req(), verity_io_real_digest()
89 * and verity_io_want_digest().
93 struct verity_result {
94 struct completion completion;
98 static inline struct ahash_request *verity_io_hash_req(struct dm_verity *v,
99 struct dm_verity_io *io)
101 return (struct ahash_request *)(io + 1);
104 static inline u8 *verity_io_real_digest(struct dm_verity *v,
105 struct dm_verity_io *io)
107 return (u8 *)(io + 1) + v->ahash_reqsize;
110 static inline u8 *verity_io_want_digest(struct dm_verity *v,
111 struct dm_verity_io *io)
113 return (u8 *)(io + 1) + v->ahash_reqsize + v->digest_size;
116 static inline u8 *verity_io_digest_end(struct dm_verity *v,
117 struct dm_verity_io *io)
119 return verity_io_want_digest(v, io) + v->digest_size;
122 extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
123 struct bvec_iter *iter,
124 int (*process)(struct dm_verity *v,
125 struct dm_verity_io *io,
126 u8 *data, size_t len));
128 extern int verity_hash(struct dm_verity *v, struct ahash_request *req,
129 const u8 *data, size_t len, u8 *digest);
131 extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
132 sector_t block, u8 *digest, bool *is_zero);
134 #endif /* DM_VERITY_H */