Merge tag 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm
[platform/kernel/linux-starfive.git] / drivers / md / dm-verity.h
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-bufio.h>
15 #include <linux/device-mapper.h>
16 #include <linux/interrupt.h>
17 #include <crypto/hash.h>
18
19 #define DM_VERITY_MAX_LEVELS            63
20
21 enum verity_mode {
22         DM_VERITY_MODE_EIO,
23         DM_VERITY_MODE_LOGGING,
24         DM_VERITY_MODE_RESTART,
25         DM_VERITY_MODE_PANIC
26 };
27
28 enum verity_block_type {
29         DM_VERITY_BLOCK_TYPE_DATA,
30         DM_VERITY_BLOCK_TYPE_METADATA
31 };
32
33 struct dm_verity_fec;
34
35 struct dm_verity {
36         struct dm_dev *data_dev;
37         struct dm_dev *hash_dev;
38         struct dm_target *ti;
39         struct dm_bufio_client *bufio;
40         char *alg_name;
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 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 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 corrupted_errs;/* Number of errors for corrupted blocks */
61
62         struct workqueue_struct *verify_wq;
63
64         /* starting blocks for each tree level. 0 is the lowest level. */
65         sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
66
67         struct dm_verity_fec *fec;      /* forward error correction */
68         unsigned long *validated_blocks; /* bitset blocks validated */
69
70         char *signature_key_desc; /* signature keyring reference */
71 };
72
73 struct dm_verity_io {
74         struct dm_verity *v;
75
76         /* original value of bio->bi_end_io */
77         bio_end_io_t *orig_bi_end_io;
78
79         sector_t block;
80         unsigned n_blocks;
81         bool in_tasklet;
82
83         struct bvec_iter iter;
84
85         struct work_struct work;
86         struct tasklet_struct tasklet;
87
88         /*
89          * Three variably-size fields follow this struct:
90          *
91          * u8 hash_req[v->ahash_reqsize];
92          * u8 real_digest[v->digest_size];
93          * u8 want_digest[v->digest_size];
94          *
95          * To access them use: verity_io_hash_req(), verity_io_real_digest()
96          * and verity_io_want_digest().
97          */
98 };
99
100 static inline struct ahash_request *verity_io_hash_req(struct dm_verity *v,
101                                                      struct dm_verity_io *io)
102 {
103         return (struct ahash_request *)(io + 1);
104 }
105
106 static inline u8 *verity_io_real_digest(struct dm_verity *v,
107                                         struct dm_verity_io *io)
108 {
109         return (u8 *)(io + 1) + v->ahash_reqsize;
110 }
111
112 static inline u8 *verity_io_want_digest(struct dm_verity *v,
113                                         struct dm_verity_io *io)
114 {
115         return (u8 *)(io + 1) + v->ahash_reqsize + v->digest_size;
116 }
117
118 static inline u8 *verity_io_digest_end(struct dm_verity *v,
119                                        struct dm_verity_io *io)
120 {
121         return verity_io_want_digest(v, io) + v->digest_size;
122 }
123
124 extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
125                                struct bvec_iter *iter,
126                                int (*process)(struct dm_verity *v,
127                                               struct dm_verity_io *io,
128                                               u8 *data, size_t len));
129
130 extern int verity_hash(struct dm_verity *v, struct ahash_request *req,
131                        const u8 *data, size_t len, u8 *digest);
132
133 extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
134                                  sector_t block, u8 *digest, bool *is_zero);
135
136 extern bool dm_is_verity_target(struct dm_target *ti);
137 extern int dm_verity_get_mode(struct dm_target *ti);
138 extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest,
139                                      unsigned int *digest_size);
140
141 #endif /* DM_VERITY_H */