dm-verity.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef DM_VERITY_H
  11. #define DM_VERITY_H
  12. #include <linux/dm-io.h>
  13. #include <linux/dm-bufio.h>
  14. #include <linux/device-mapper.h>
  15. #include <linux/interrupt.h>
  16. #include <crypto/hash.h>
  17. #define DM_VERITY_MAX_LEVELS 63
  18. enum verity_mode {
  19. DM_VERITY_MODE_EIO,
  20. DM_VERITY_MODE_LOGGING,
  21. DM_VERITY_MODE_RESTART,
  22. DM_VERITY_MODE_PANIC
  23. };
  24. enum verity_block_type {
  25. DM_VERITY_BLOCK_TYPE_DATA,
  26. DM_VERITY_BLOCK_TYPE_METADATA
  27. };
  28. struct dm_verity_fec;
  29. struct dm_verity {
  30. struct dm_dev *data_dev;
  31. struct dm_dev *hash_dev;
  32. struct dm_target *ti;
  33. struct dm_bufio_client *bufio;
  34. char *alg_name;
  35. struct crypto_ahash *ahash_tfm; /* either this or shash_tfm is set */
  36. struct crypto_shash *shash_tfm; /* either this or ahash_tfm is set */
  37. u8 *root_digest; /* digest of the root block */
  38. u8 *salt; /* salt: its size is salt_size */
  39. u8 *initial_hashstate; /* salted initial state, if shash_tfm is set */
  40. u8 *zero_digest; /* digest for a zero block */
  41. #ifdef CONFIG_SECURITY
  42. u8 *root_digest_sig; /* signature of the root digest */
  43. unsigned int sig_size; /* root digest signature size */
  44. #endif /* CONFIG_SECURITY */
  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_bh_wq:1; /* try to verify in BH wq before normal work-queue */
  57. unsigned int digest_size; /* digest size for the current hash algorithm */
  58. unsigned int hash_reqsize; /* the size of temporary space for crypto */
  59. enum verity_mode mode; /* mode for handling verification errors */
  60. enum verity_mode error_mode;/* mode for handling I/O errors */
  61. unsigned int corrupted_errs;/* Number of errors for corrupted blocks */
  62. struct workqueue_struct *verify_wq;
  63. /* starting blocks for each tree level. 0 is the lowest level. */
  64. sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
  65. struct dm_verity_fec *fec; /* forward error correction */
  66. unsigned long *validated_blocks; /* bitset blocks validated */
  67. char *signature_key_desc; /* signature keyring reference */
  68. struct dm_io_client *io;
  69. mempool_t recheck_pool;
  70. };
  71. struct dm_verity_io {
  72. struct dm_verity *v;
  73. /* original value of bio->bi_end_io */
  74. bio_end_io_t *orig_bi_end_io;
  75. struct bvec_iter iter;
  76. sector_t block;
  77. unsigned int n_blocks;
  78. bool in_bh;
  79. bool had_mismatch;
  80. struct work_struct work;
  81. struct work_struct bh_work;
  82. u8 real_digest[HASH_MAX_DIGESTSIZE];
  83. u8 want_digest[HASH_MAX_DIGESTSIZE];
  84. /*
  85. * This struct is followed by a variable-sized hash request of size
  86. * v->hash_reqsize, either a struct ahash_request or a struct shash_desc
  87. * (depending on whether ahash_tfm or shash_tfm is being used). To
  88. * access it, use verity_io_hash_req().
  89. */
  90. };
  91. static inline void *verity_io_hash_req(struct dm_verity *v,
  92. struct dm_verity_io *io)
  93. {
  94. return io + 1;
  95. }
  96. static inline u8 *verity_io_real_digest(struct dm_verity *v,
  97. struct dm_verity_io *io)
  98. {
  99. return io->real_digest;
  100. }
  101. static inline u8 *verity_io_want_digest(struct dm_verity *v,
  102. struct dm_verity_io *io)
  103. {
  104. return io->want_digest;
  105. }
  106. extern int verity_hash(struct dm_verity *v, struct dm_verity_io *io,
  107. const u8 *data, size_t len, u8 *digest, bool may_sleep);
  108. extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
  109. sector_t block, u8 *digest, bool *is_zero);
  110. extern bool dm_is_verity_target(struct dm_target *ti);
  111. extern int dm_verity_get_mode(struct dm_target *ti);
  112. extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest,
  113. unsigned int *digest_size);
  114. #endif /* DM_VERITY_H */