luks.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2003,2007,2010,2011,2019 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. #include <grub/cryptodisk.h>
  19. #include <grub/types.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/dl.h>
  23. #include <grub/err.h>
  24. #include <grub/disk.h>
  25. #include <grub/crypto.h>
  26. #include <grub/partition.h>
  27. #include <grub/i18n.h>
  28. GRUB_MOD_LICENSE ("GPLv3+");
  29. #define LUKS_KEY_ENABLED 0x00AC71F3
  30. /* On disk LUKS header */
  31. struct grub_luks_phdr
  32. {
  33. grub_uint8_t magic[6];
  34. #define LUKS_MAGIC "LUKS\xBA\xBE"
  35. grub_uint16_t version;
  36. char cipherName[32];
  37. char cipherMode[32];
  38. char hashSpec[32];
  39. grub_uint32_t payloadOffset;
  40. grub_uint32_t keyBytes;
  41. grub_uint8_t mkDigest[20];
  42. grub_uint8_t mkDigestSalt[32];
  43. grub_uint32_t mkDigestIterations;
  44. char uuid[40];
  45. struct
  46. {
  47. grub_uint32_t active;
  48. grub_uint32_t passwordIterations;
  49. grub_uint8_t passwordSalt[32];
  50. grub_uint32_t keyMaterialOffset;
  51. grub_uint32_t stripes;
  52. } keyblock[8];
  53. } GRUB_PACKED;
  54. typedef struct grub_luks_phdr *grub_luks_phdr_t;
  55. gcry_err_code_t AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src,
  56. grub_uint8_t * dst, grub_size_t blocksize,
  57. grub_size_t blocknumbers);
  58. static grub_cryptodisk_t
  59. luks_scan (grub_disk_t disk, grub_cryptomount_args_t cargs)
  60. {
  61. grub_cryptodisk_t newdev;
  62. struct grub_luks_phdr header;
  63. char ciphername[sizeof (header.cipherName) + 1];
  64. char ciphermode[sizeof (header.cipherMode) + 1];
  65. char hashspec[sizeof (header.hashSpec) + 1];
  66. grub_err_t err;
  67. if (cargs->check_boot)
  68. return NULL;
  69. /* Read the LUKS header. */
  70. err = grub_disk_read (disk, 0, 0, sizeof (header), &header);
  71. if (err)
  72. {
  73. if (err == GRUB_ERR_OUT_OF_RANGE)
  74. grub_errno = GRUB_ERR_NONE;
  75. return NULL;
  76. }
  77. /* Look for LUKS magic sequence. */
  78. if (grub_memcmp (header.magic, LUKS_MAGIC, sizeof (header.magic))
  79. || grub_be_to_cpu16 (header.version) != 1)
  80. return NULL;
  81. if (cargs->search_uuid != NULL && grub_uuidcasecmp (cargs->search_uuid, header.uuid, sizeof (header.uuid)) != 0)
  82. {
  83. grub_dprintf ("luks", "%s != %s\n", header.uuid, cargs->search_uuid);
  84. return NULL;
  85. }
  86. /* Make sure that strings are null terminated. */
  87. grub_memcpy (ciphername, header.cipherName, sizeof (header.cipherName));
  88. ciphername[sizeof (header.cipherName)] = 0;
  89. grub_memcpy (ciphermode, header.cipherMode, sizeof (header.cipherMode));
  90. ciphermode[sizeof (header.cipherMode)] = 0;
  91. grub_memcpy (hashspec, header.hashSpec, sizeof (header.hashSpec));
  92. hashspec[sizeof (header.hashSpec)] = 0;
  93. newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
  94. if (!newdev)
  95. return NULL;
  96. newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
  97. newdev->source_disk = NULL;
  98. newdev->log_sector_size = GRUB_LUKS1_LOG_SECTOR_SIZE;
  99. newdev->total_sectors = grub_disk_native_sectors (disk) - newdev->offset_sectors;
  100. grub_memcpy (newdev->uuid, header.uuid, sizeof (header.uuid));
  101. newdev->modname = "luks";
  102. /* Configure the hash used for the AF splitter and HMAC. */
  103. newdev->hash = grub_crypto_lookup_md_by_name (hashspec);
  104. if (!newdev->hash)
  105. {
  106. grub_free (newdev);
  107. grub_error (GRUB_ERR_FILE_NOT_FOUND, "Couldn't load %s hash",
  108. hashspec);
  109. return NULL;
  110. }
  111. err = grub_cryptodisk_setcipher (newdev, ciphername, ciphermode);
  112. if (err)
  113. {
  114. grub_free (newdev);
  115. return NULL;
  116. }
  117. COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= sizeof (header.uuid));
  118. return newdev;
  119. }
  120. static grub_err_t
  121. luks_recover_key (grub_disk_t source,
  122. grub_cryptodisk_t dev,
  123. grub_cryptomount_args_t cargs)
  124. {
  125. struct grub_luks_phdr header;
  126. grub_size_t keysize;
  127. grub_uint8_t *split_key = NULL;
  128. grub_uint8_t candidate_digest[sizeof (header.mkDigest)];
  129. unsigned i;
  130. grub_size_t length;
  131. grub_err_t err;
  132. grub_size_t max_stripes = 1;
  133. if (cargs->key_data == NULL || cargs->key_len == 0)
  134. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no key data");
  135. err = grub_disk_read (source, 0, 0, sizeof (header), &header);
  136. if (err)
  137. return err;
  138. grub_puts_ (N_("Attempting to decrypt master key..."));
  139. keysize = grub_be_to_cpu32 (header.keyBytes);
  140. if (keysize > GRUB_CRYPTODISK_MAX_KEYLEN)
  141. return grub_error (GRUB_ERR_BAD_FS, "key is too long");
  142. for (i = 0; i < ARRAY_SIZE (header.keyblock); i++)
  143. if (grub_be_to_cpu32 (header.keyblock[i].active) == LUKS_KEY_ENABLED
  144. && grub_be_to_cpu32 (header.keyblock[i].stripes) > max_stripes)
  145. max_stripes = grub_be_to_cpu32 (header.keyblock[i].stripes);
  146. split_key = grub_calloc (keysize, max_stripes);
  147. if (!split_key)
  148. return grub_errno;
  149. /* Try to recover master key from each active keyslot. */
  150. for (i = 0; i < ARRAY_SIZE (header.keyblock); i++)
  151. {
  152. gcry_err_code_t gcry_err;
  153. grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
  154. grub_uint8_t digest[GRUB_CRYPTODISK_MAX_KEYLEN];
  155. /* Check if keyslot is enabled. */
  156. if (grub_be_to_cpu32 (header.keyblock[i].active) != LUKS_KEY_ENABLED)
  157. continue;
  158. grub_dprintf ("luks", "Trying keyslot %d\n", i);
  159. /* Calculate the PBKDF2 of the user supplied passphrase. */
  160. gcry_err = grub_crypto_pbkdf2 (dev->hash, cargs->key_data,
  161. cargs->key_len,
  162. header.keyblock[i].passwordSalt,
  163. sizeof (header.keyblock[i].passwordSalt),
  164. grub_be_to_cpu32 (header.keyblock[i].
  165. passwordIterations),
  166. digest, keysize);
  167. if (gcry_err)
  168. {
  169. grub_free (split_key);
  170. return grub_crypto_gcry_error (gcry_err);
  171. }
  172. grub_dprintf ("luks", "PBKDF2 done\n");
  173. gcry_err = grub_cryptodisk_setkey (dev, digest, keysize);
  174. if (gcry_err)
  175. {
  176. grub_free (split_key);
  177. return grub_crypto_gcry_error (gcry_err);
  178. }
  179. length = (keysize * grub_be_to_cpu32 (header.keyblock[i].stripes));
  180. /* Read and decrypt the key material from the disk. */
  181. err = grub_disk_read (source,
  182. grub_be_to_cpu32 (header.keyblock
  183. [i].keyMaterialOffset), 0,
  184. length, split_key);
  185. if (err)
  186. {
  187. grub_free (split_key);
  188. return err;
  189. }
  190. gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
  191. GRUB_LUKS1_LOG_SECTOR_SIZE);
  192. if (gcry_err)
  193. {
  194. grub_free (split_key);
  195. return grub_crypto_gcry_error (gcry_err);
  196. }
  197. /* Merge the decrypted key material to get the candidate master key. */
  198. gcry_err = AF_merge (dev->hash, split_key, candidate_key, keysize,
  199. grub_be_to_cpu32 (header.keyblock[i].stripes));
  200. if (gcry_err)
  201. {
  202. grub_free (split_key);
  203. return grub_crypto_gcry_error (gcry_err);
  204. }
  205. grub_dprintf ("luks", "candidate key recovered\n");
  206. /* Calculate the PBKDF2 of the candidate master key. */
  207. gcry_err = grub_crypto_pbkdf2 (dev->hash, candidate_key,
  208. grub_be_to_cpu32 (header.keyBytes),
  209. header.mkDigestSalt,
  210. sizeof (header.mkDigestSalt),
  211. grub_be_to_cpu32
  212. (header.mkDigestIterations),
  213. candidate_digest,
  214. sizeof (candidate_digest));
  215. if (gcry_err)
  216. {
  217. grub_free (split_key);
  218. return grub_crypto_gcry_error (gcry_err);
  219. }
  220. /* Compare the calculated PBKDF2 to the digest stored
  221. in the header to see if it's correct. */
  222. if (grub_memcmp (candidate_digest, header.mkDigest,
  223. sizeof (header.mkDigest)) != 0)
  224. {
  225. grub_dprintf ("luks", "bad digest\n");
  226. continue;
  227. }
  228. /* TRANSLATORS: It's a cryptographic key slot: one element of an array
  229. where each element is either empty or holds a key. */
  230. grub_printf_ (N_("Slot %d opened\n"), i);
  231. /* Set the master key. */
  232. gcry_err = grub_cryptodisk_setkey (dev, candidate_key, keysize);
  233. if (gcry_err)
  234. {
  235. grub_free (split_key);
  236. return grub_crypto_gcry_error (gcry_err);
  237. }
  238. grub_free (split_key);
  239. return GRUB_ERR_NONE;
  240. }
  241. grub_free (split_key);
  242. return GRUB_ACCESS_DENIED;
  243. }
  244. struct grub_cryptodisk_dev luks_crypto = {
  245. .scan = luks_scan,
  246. .recover_key = luks_recover_key
  247. };
  248. GRUB_MOD_INIT (luks)
  249. {
  250. COMPILE_TIME_ASSERT (sizeof (((struct grub_luks_phdr *) 0)->uuid)
  251. < GRUB_CRYPTODISK_MAX_UUID_LENGTH);
  252. grub_cryptodisk_dev_register (&luks_crypto);
  253. }
  254. GRUB_MOD_FINI (luks)
  255. {
  256. grub_cryptodisk_dev_unregister (&luks_crypto);
  257. }