dm-verity.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <linux/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_ahash *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 int ahash_reqsize;/* 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. unsigned long *validated_blocks; /* bitset blocks validated */
  57. };
  58. struct dm_verity_io {
  59. struct dm_verity *v;
  60. /* original value of bio->bi_end_io */
  61. bio_end_io_t *orig_bi_end_io;
  62. sector_t block;
  63. unsigned n_blocks;
  64. struct bvec_iter iter;
  65. struct work_struct work;
  66. /*
  67. * Three variably-size fields follow this struct:
  68. *
  69. * u8 hash_req[v->ahash_reqsize];
  70. * u8 real_digest[v->digest_size];
  71. * u8 want_digest[v->digest_size];
  72. *
  73. * To access them use: verity_io_hash_req(), verity_io_real_digest()
  74. * and verity_io_want_digest().
  75. */
  76. };
  77. static inline struct ahash_request *verity_io_hash_req(struct dm_verity *v,
  78. struct dm_verity_io *io)
  79. {
  80. return (struct ahash_request *)(io + 1);
  81. }
  82. static inline u8 *verity_io_real_digest(struct dm_verity *v,
  83. struct dm_verity_io *io)
  84. {
  85. return (u8 *)(io + 1) + v->ahash_reqsize;
  86. }
  87. static inline u8 *verity_io_want_digest(struct dm_verity *v,
  88. struct dm_verity_io *io)
  89. {
  90. return (u8 *)(io + 1) + v->ahash_reqsize + v->digest_size;
  91. }
  92. static inline u8 *verity_io_digest_end(struct dm_verity *v,
  93. struct dm_verity_io *io)
  94. {
  95. return verity_io_want_digest(v, io) + v->digest_size;
  96. }
  97. extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
  98. struct bvec_iter *iter,
  99. int (*process)(struct dm_verity *v,
  100. struct dm_verity_io *io,
  101. u8 *data, size_t len));
  102. extern int verity_hash(struct dm_verity *v, struct ahash_request *req,
  103. const u8 *data, size_t len, u8 *digest);
  104. extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
  105. sector_t block, u8 *digest, bool *is_zero);
  106. #endif /* DM_VERITY_H */