cryptodisk.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_CRYPTODISK_HEADER
  19. #define GRUB_CRYPTODISK_HEADER 1
  20. #include <grub/disk.h>
  21. #include <grub/file.h>
  22. #include <grub/crypto.h>
  23. #include <grub/list.h>
  24. #ifdef GRUB_UTIL
  25. #include <grub/emu/hostdisk.h>
  26. #endif
  27. typedef enum
  28. {
  29. GRUB_CRYPTODISK_MODE_ECB,
  30. GRUB_CRYPTODISK_MODE_CBC,
  31. GRUB_CRYPTODISK_MODE_PCBC,
  32. GRUB_CRYPTODISK_MODE_XTS,
  33. GRUB_CRYPTODISK_MODE_LRW
  34. } grub_cryptodisk_mode_t;
  35. typedef enum
  36. {
  37. GRUB_CRYPTODISK_MODE_IV_NULL,
  38. GRUB_CRYPTODISK_MODE_IV_PLAIN,
  39. GRUB_CRYPTODISK_MODE_IV_PLAIN64,
  40. GRUB_CRYPTODISK_MODE_IV_ESSIV,
  41. GRUB_CRYPTODISK_MODE_IV_BENBI,
  42. GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64,
  43. GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64_HASH
  44. } grub_cryptodisk_mode_iv_t;
  45. #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
  46. /* LUKS1 specification defines the block size to always be 512 bytes. */
  47. #define GRUB_LUKS1_LOG_SECTOR_SIZE 9
  48. /* By default dm-crypt increments the IV every 512 bytes. */
  49. #define GRUB_CRYPTODISK_IV_LOG_SIZE 9
  50. #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
  51. #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
  52. #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
  53. #define GRUB_CRYPTODISK_GF_BYTES (1U << GRUB_CRYPTODISK_GF_LOG_BYTES)
  54. #define GRUB_CRYPTODISK_MAX_KEYLEN 128
  55. #define GRUB_CRYPTODISK_MAX_PASSPHRASE 256
  56. #define GRUB_CRYPTODISK_MAX_KEYFILE_SIZE 8192
  57. struct grub_cryptodisk;
  58. typedef gcry_err_code_t
  59. (*grub_cryptodisk_rekey_func_t) (struct grub_cryptodisk *dev,
  60. grub_uint64_t zoneno);
  61. struct grub_cryptomount_cached_key
  62. {
  63. grub_uint8_t *key;
  64. grub_size_t key_len;
  65. /*
  66. * The key protector associated with this cache entry failed, so avoid it
  67. * even if the cached entry (an instance of this structure) is empty.
  68. */
  69. bool invalid;
  70. };
  71. struct grub_cryptomount_args
  72. {
  73. /* scan: Flag to indicate that only bootable volumes should be decrypted */
  74. grub_uint32_t check_boot : 1;
  75. /* scan: Only volumes matching this UUID should be decrpyted */
  76. char *search_uuid;
  77. /* recover_key: Key data used to decrypt voume */
  78. grub_uint8_t *key_data;
  79. /* recover_key: Length of key_data */
  80. grub_size_t key_len;
  81. grub_file_t hdr_file;
  82. /* recover_key: Names of the key protectors to use (NULL-terminated) */
  83. char **protectors;
  84. /* recover_key: Key cache to avoid invoking the same key protector twice */
  85. struct grub_cryptomount_cached_key *key_cache;
  86. };
  87. typedef struct grub_cryptomount_args *grub_cryptomount_args_t;
  88. struct grub_cryptodisk
  89. {
  90. struct grub_cryptodisk *next;
  91. struct grub_cryptodisk **prev;
  92. char *source;
  93. /*
  94. * The number of sectors the start of the encrypted data is offset into the
  95. * underlying disk, where sectors are the size noted by log_sector_size.
  96. */
  97. grub_disk_addr_t offset_sectors;
  98. /* Total number of encrypted sectors of size (1 << log_sector_size). */
  99. grub_disk_addr_t total_sectors;
  100. grub_disk_t source_disk;
  101. int ref;
  102. grub_crypto_cipher_handle_t cipher;
  103. grub_crypto_cipher_handle_t secondary_cipher;
  104. grub_crypto_cipher_handle_t essiv_cipher;
  105. const gcry_md_spec_t *essiv_hash, *hash, *iv_hash;
  106. grub_cryptodisk_mode_t mode;
  107. grub_cryptodisk_mode_iv_t mode_iv;
  108. int benbi_log;
  109. unsigned long id, source_id;
  110. enum grub_disk_dev_id source_dev_id;
  111. char uuid[GRUB_CRYPTODISK_MAX_UUID_LENGTH + 1];
  112. grub_uint8_t lrw_key[GRUB_CRYPTODISK_GF_BYTES];
  113. grub_uint8_t *lrw_precalc;
  114. grub_uint8_t iv_prefix[64];
  115. grub_size_t iv_prefix_len;
  116. grub_uint8_t key[GRUB_CRYPTODISK_MAX_KEYLEN];
  117. grub_size_t keysize;
  118. #ifdef GRUB_UTIL
  119. char *cheat;
  120. grub_util_fd_t cheat_fd;
  121. #endif
  122. const char *modname;
  123. int log_sector_size;
  124. grub_cryptodisk_rekey_func_t rekey;
  125. int rekey_shift;
  126. grub_uint8_t rekey_key[64];
  127. grub_uint64_t last_rekey;
  128. int rekey_derived_size;
  129. grub_disk_addr_t partition_start;
  130. };
  131. typedef struct grub_cryptodisk *grub_cryptodisk_t;
  132. struct grub_cryptodisk_dev
  133. {
  134. struct grub_cryptodisk_dev *next;
  135. struct grub_cryptodisk_dev **prev;
  136. grub_cryptodisk_t (*scan) (grub_disk_t disk, grub_cryptomount_args_t cargs);
  137. grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev, grub_cryptomount_args_t cargs);
  138. };
  139. typedef struct grub_cryptodisk_dev *grub_cryptodisk_dev_t;
  140. extern grub_cryptodisk_dev_t EXPORT_VAR (grub_cryptodisk_list);
  141. #ifndef GRUB_LST_GENERATOR
  142. static inline void
  143. grub_cryptodisk_dev_register (grub_cryptodisk_dev_t cr)
  144. {
  145. grub_list_push (GRUB_AS_LIST_P (&grub_cryptodisk_list), GRUB_AS_LIST (cr));
  146. }
  147. #endif
  148. static inline void
  149. grub_cryptodisk_dev_unregister (grub_cryptodisk_dev_t cr)
  150. {
  151. grub_list_remove (GRUB_AS_LIST (cr));
  152. }
  153. #define FOR_CRYPTODISK_DEVS(var) FOR_LIST_ELEMENTS((var), (grub_cryptodisk_list))
  154. grub_err_t
  155. grub_cryptodisk_setcipher (grub_cryptodisk_t crypt, const char *ciphername, const char *ciphermode);
  156. gcry_err_code_t
  157. grub_cryptodisk_setkey (grub_cryptodisk_t dev,
  158. grub_uint8_t *key, grub_size_t keysize);
  159. gcry_err_code_t
  160. grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
  161. grub_uint8_t * data, grub_size_t len,
  162. grub_disk_addr_t sector, grub_size_t log_sector_size);
  163. grub_err_t
  164. grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
  165. grub_disk_t source);
  166. #ifdef GRUB_UTIL
  167. grub_err_t
  168. grub_cryptodisk_cheat_insert (grub_cryptodisk_t newdev, const char *name,
  169. grub_disk_t source, const char *cheat);
  170. void
  171. grub_util_cryptodisk_get_abstraction (grub_disk_t disk,
  172. void (*cb) (const char *val, void *data),
  173. void *data);
  174. char *
  175. grub_util_get_geli_uuid (const char *dev);
  176. #endif
  177. grub_cryptodisk_t grub_cryptodisk_get_by_uuid (const char *uuid);
  178. grub_cryptodisk_t grub_cryptodisk_get_by_source_disk (grub_disk_t disk);
  179. #ifdef GRUB_MACHINE_EFI
  180. grub_err_t grub_cryptodisk_challenge_password (void);
  181. #endif
  182. #endif