dm-verity.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include <crypto/sha2.h>
  18. #define DM_VERITY_MAX_LEVELS 63
  19. enum verity_mode {
  20. DM_VERITY_MODE_EIO,
  21. DM_VERITY_MODE_LOGGING,
  22. DM_VERITY_MODE_RESTART,
  23. DM_VERITY_MODE_PANIC
  24. };
  25. enum verity_block_type {
  26. DM_VERITY_BLOCK_TYPE_DATA,
  27. DM_VERITY_BLOCK_TYPE_METADATA
  28. };
  29. struct dm_verity_fec;
  30. struct dm_verity {
  31. struct dm_dev *data_dev;
  32. struct dm_dev *hash_dev;
  33. struct dm_target *ti;
  34. struct dm_bufio_client *bufio;
  35. char *alg_name;
  36. struct crypto_shash *shash_tfm;
  37. u8 *root_digest; /* digest of the root block */
  38. u8 *salt; /* salt: its size is salt_size */
  39. union {
  40. struct sha256_ctx *sha256; /* for use_sha256_lib=1 */
  41. u8 *shash; /* for use_sha256_lib=0 */
  42. } initial_hashstate; /* salted initial state, if version >= 1 */
  43. u8 *zero_digest; /* digest for a zero block */
  44. #ifdef CONFIG_SECURITY
  45. u8 *root_digest_sig; /* signature of the root digest */
  46. unsigned int sig_size; /* root digest signature size */
  47. #endif /* CONFIG_SECURITY */
  48. unsigned int salt_size;
  49. sector_t hash_start; /* hash start in blocks */
  50. sector_t data_blocks; /* the number of data blocks */
  51. sector_t hash_blocks; /* the number of hash blocks */
  52. unsigned char data_dev_block_bits; /* log2(data blocksize) */
  53. unsigned char hash_dev_block_bits; /* log2(hash blocksize) */
  54. unsigned char hash_per_block_bits; /* log2(hashes in hash block) */
  55. unsigned char levels; /* the number of tree levels */
  56. unsigned char version;
  57. bool hash_failed:1; /* set if hash of any block failed */
  58. bool use_bh_wq:1; /* try to verify in BH wq before normal work-queue */
  59. bool use_sha256_lib:1; /* use SHA-256 library instead of generic crypto API */
  60. bool use_sha256_finup_2x:1; /* use interleaved hashing optimization */
  61. unsigned int digest_size; /* digest size for the current hash algorithm */
  62. enum verity_mode mode; /* mode for handling verification errors */
  63. enum verity_mode error_mode;/* mode for handling I/O errors */
  64. unsigned int corrupted_errs;/* Number of errors for corrupted blocks */
  65. struct workqueue_struct *verify_wq;
  66. /* starting blocks for each tree level. 0 is the lowest level. */
  67. sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
  68. struct dm_verity_fec *fec; /* forward error correction */
  69. unsigned long *validated_blocks; /* bitset blocks validated */
  70. char *signature_key_desc; /* signature keyring reference */
  71. struct dm_io_client *io;
  72. mempool_t recheck_pool;
  73. };
  74. struct pending_block {
  75. void *data;
  76. sector_t blkno;
  77. u8 want_digest[HASH_MAX_DIGESTSIZE];
  78. u8 real_digest[HASH_MAX_DIGESTSIZE];
  79. };
  80. struct dm_verity_io {
  81. struct dm_verity *v;
  82. /* original value of bio->bi_end_io */
  83. bio_end_io_t *orig_bi_end_io;
  84. struct bvec_iter iter;
  85. sector_t block;
  86. unsigned int n_blocks;
  87. bool in_bh;
  88. bool had_mismatch;
  89. #ifdef CONFIG_DM_VERITY_FEC
  90. struct dm_verity_fec_io *fec_io;
  91. #endif
  92. struct work_struct work;
  93. u8 tmp_digest[HASH_MAX_DIGESTSIZE];
  94. /*
  95. * This is the queue of data blocks that are pending verification. When
  96. * the crypto layer supports interleaved hashing, we allow multiple
  97. * blocks to be queued up in order to utilize it. This can improve
  98. * performance significantly vs. sequential hashing of each block.
  99. */
  100. int num_pending;
  101. struct pending_block pending_blocks[2];
  102. /*
  103. * Temporary space for hashing. Either sha256 or shash is used,
  104. * depending on the value of use_sha256_lib. If shash is used,
  105. * then this field is variable-length, with total size
  106. * sizeof(struct shash_desc) + crypto_shash_descsize(shash_tfm).
  107. * For this reason, this field must be the end of the struct.
  108. */
  109. union {
  110. struct sha256_ctx sha256;
  111. struct shash_desc shash;
  112. } hash_ctx;
  113. };
  114. extern int verity_hash(struct dm_verity *v, struct dm_verity_io *io,
  115. const u8 *data, size_t len, u8 *digest);
  116. extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
  117. sector_t block, u8 *digest, bool *is_zero);
  118. extern bool dm_is_verity_target(struct dm_target *ti);
  119. extern int dm_verity_get_mode(struct dm_target *ti);
  120. extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest,
  121. unsigned int *digest_size);
  122. #endif /* DM_VERITY_H */