luks.c 9.3 KB

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