dm-verity.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2012 Red Hat, Inc.
  3. * Copyright (C) 2015 Google, Inc.
  4. *
  5. * Author: Mikulas Patocka <mpatocka@redhat.com>
  6. *
  7. * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
  8. *
  9. * This file is released under the GPLv2.
  10. */
  11. #ifndef DM_VERITY_H
  12. #define DM_VERITY_H
  13. #include "dm-bufio.h"
  14. #include <linux/device-mapper.h>
  15. #include <crypto/hash.h>
  16. #define DM_VERITY_MAX_LEVELS 63
  17. enum verity_mode {
  18. DM_VERITY_MODE_EIO,
  19. DM_VERITY_MODE_LOGGING,
  20. DM_VERITY_MODE_RESTART
  21. };
  22. enum verity_block_type {
  23. DM_VERITY_BLOCK_TYPE_DATA,
  24. DM_VERITY_BLOCK_TYPE_METADATA
  25. };
  26. struct dm_verity_fec;
  27. struct dm_verity {
  28. struct dm_dev *data_dev;
  29. struct dm_dev *hash_dev;
  30. struct dm_target *ti;
  31. struct dm_bufio_client *bufio;
  32. char *alg_name;
  33. struct crypto_shash *tfm;
  34. u8 *root_digest; /* digest of the root block */
  35. u8 *salt; /* salt: its size is salt_size */
  36. u8 *zero_digest; /* digest for a zero block */
  37. unsigned salt_size;
  38. sector_t data_start; /* data offset in 512-byte sectors */
  39. sector_t hash_start; /* hash start in blocks */
  40. sector_t data_blocks; /* the number of data blocks */
  41. sector_t hash_blocks; /* the number of hash blocks */
  42. unsigned char data_dev_block_bits; /* log2(data blocksize) */
  43. unsigned char hash_dev_block_bits; /* log2(hash blocksize) */
  44. unsigned char hash_per_block_bits; /* log2(hashes in hash block) */
  45. unsigned char levels; /* the number of tree levels */
  46. unsigned char version;
  47. unsigned digest_size; /* digest size for the current hash algorithm */
  48. unsigned shash_descsize;/* the size of temporary space for crypto */
  49. int hash_failed; /* set to 1 if hash of any block failed */
  50. enum verity_mode mode; /* mode for handling verification errors */
  51. unsigned corrupted_errs;/* Number of errors for corrupted blocks */
  52. struct workqueue_struct *verify_wq;
  53. /* starting blocks for each tree level. 0 is the lowest level. */
  54. sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
  55. struct dm_verity_fec *fec; /* forward error correction */
  56. };
  57. struct dm_verity_io {
  58. struct dm_verity *v;
  59. /* original value of bio->bi_end_io */
  60. bio_end_io_t *orig_bi_end_io;
  61. sector_t block;
  62. unsigned n_blocks;
  63. struct bvec_iter iter;
  64. struct work_struct work;
  65. /*
  66. * Three variably-size fields follow this struct:
  67. *
  68. * u8 hash_desc[v->shash_descsize];
  69. * u8 real_digest[v->digest_size];
  70. * u8 want_digest[v->digest_size];
  71. *
  72. * To access them use: verity_io_hash_desc(), verity_io_real_digest()
  73. * and verity_io_want_digest().
  74. */
  75. };
  76. static inline struct shash_desc *verity_io_hash_desc(struct dm_verity *v,
  77. struct dm_verity_io *io)
  78. {
  79. return (struct shash_desc *)(io + 1);
  80. }
  81. static inline u8 *verity_io_real_digest(struct dm_verity *v,
  82. struct dm_verity_io *io)
  83. {
  84. return (u8 *)(io + 1) + v->shash_descsize;
  85. }
  86. static inline u8 *verity_io_want_digest(struct dm_verity *v,
  87. struct dm_verity_io *io)
  88. {
  89. return (u8 *)(io + 1) + v->shash_descsize + v->digest_size;
  90. }
  91. static inline u8 *verity_io_digest_end(struct dm_verity *v,
  92. struct dm_verity_io *io)
  93. {
  94. return verity_io_want_digest(v, io) + v->digest_size;
  95. }
  96. extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
  97. struct bvec_iter *iter,
  98. int (*process)(struct dm_verity *v,
  99. struct dm_verity_io *io,
  100. u8 *data, size_t len));
  101. extern int verity_hash(struct dm_verity *v, struct shash_desc *desc,
  102. const u8 *data, size_t len, u8 *digest);
  103. extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
  104. sector_t block, u8 *digest, bool *is_zero);
  105. #endif /* DM_VERITY_H */