luks.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2003,2007,2010,2011 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 *cipheriv = NULL;
  71. char hashspec[sizeof (header.hashSpec) + 1];
  72. grub_crypto_cipher_handle_t cipher = NULL, secondary_cipher = NULL;
  73. grub_crypto_cipher_handle_t essiv_cipher = NULL;
  74. const gcry_md_spec_t *hash = NULL, *essiv_hash = NULL;
  75. const struct gcry_cipher_spec *ciph;
  76. grub_cryptodisk_mode_t mode;
  77. grub_cryptodisk_mode_iv_t mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN64;
  78. int benbi_log = 0;
  79. grub_err_t err;
  80. if (check_boot)
  81. return NULL;
  82. /* Read the LUKS header. */
  83. err = grub_disk_read (disk, 0, 0, sizeof (header), &header);
  84. if (err)
  85. {
  86. if (err == GRUB_ERR_OUT_OF_RANGE)
  87. grub_errno = GRUB_ERR_NONE;
  88. return NULL;
  89. }
  90. /* Look for LUKS magic sequence. */
  91. if (grub_memcmp (header.magic, LUKS_MAGIC, sizeof (header.magic))
  92. || grub_be_to_cpu16 (header.version) != 1)
  93. return NULL;
  94. optr = uuid;
  95. for (iptr = header.uuid; iptr < &header.uuid[ARRAY_SIZE (header.uuid)];
  96. iptr++)
  97. {
  98. if (*iptr != '-')
  99. *optr++ = *iptr;
  100. }
  101. *optr = 0;
  102. if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0)
  103. {
  104. grub_dprintf ("luks", "%s != %s\n", uuid, check_uuid);
  105. return NULL;
  106. }
  107. /* Make sure that strings are null terminated. */
  108. grub_memcpy (ciphername, header.cipherName, sizeof (header.cipherName));
  109. ciphername[sizeof (header.cipherName)] = 0;
  110. grub_memcpy (ciphermode, header.cipherMode, sizeof (header.cipherMode));
  111. ciphermode[sizeof (header.cipherMode)] = 0;
  112. grub_memcpy (hashspec, header.hashSpec, sizeof (header.hashSpec));
  113. hashspec[sizeof (header.hashSpec)] = 0;
  114. ciph = grub_crypto_lookup_cipher_by_name (ciphername);
  115. if (!ciph)
  116. {
  117. grub_error (GRUB_ERR_FILE_NOT_FOUND, "Cipher %s isn't available",
  118. ciphername);
  119. return NULL;
  120. }
  121. /* Configure the cipher used for the bulk data. */
  122. cipher = grub_crypto_cipher_open (ciph);
  123. if (!cipher)
  124. return NULL;
  125. if (grub_be_to_cpu32 (header.keyBytes) > 1024)
  126. {
  127. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid keysize %d",
  128. grub_be_to_cpu32 (header.keyBytes));
  129. grub_crypto_cipher_close (cipher);
  130. return NULL;
  131. }
  132. /* Configure the cipher mode. */
  133. if (grub_strcmp (ciphermode, "ecb") == 0)
  134. {
  135. mode = GRUB_CRYPTODISK_MODE_ECB;
  136. mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
  137. cipheriv = NULL;
  138. }
  139. else if (grub_strcmp (ciphermode, "plain") == 0)
  140. {
  141. mode = GRUB_CRYPTODISK_MODE_CBC;
  142. mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
  143. cipheriv = NULL;
  144. }
  145. else if (grub_memcmp (ciphermode, "cbc-", sizeof ("cbc-") - 1) == 0)
  146. {
  147. mode = GRUB_CRYPTODISK_MODE_CBC;
  148. cipheriv = ciphermode + sizeof ("cbc-") - 1;
  149. }
  150. else if (grub_memcmp (ciphermode, "pcbc-", sizeof ("pcbc-") - 1) == 0)
  151. {
  152. mode = GRUB_CRYPTODISK_MODE_PCBC;
  153. cipheriv = ciphermode + sizeof ("pcbc-") - 1;
  154. }
  155. else if (grub_memcmp (ciphermode, "xts-", sizeof ("xts-") - 1) == 0)
  156. {
  157. mode = GRUB_CRYPTODISK_MODE_XTS;
  158. cipheriv = ciphermode + sizeof ("xts-") - 1;
  159. secondary_cipher = grub_crypto_cipher_open (ciph);
  160. if (!secondary_cipher)
  161. {
  162. grub_crypto_cipher_close (cipher);
  163. return NULL;
  164. }
  165. if (cipher->cipher->blocksize != GRUB_CRYPTODISK_GF_BYTES)
  166. {
  167. grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported XTS block size: %d",
  168. cipher->cipher->blocksize);
  169. grub_crypto_cipher_close (cipher);
  170. grub_crypto_cipher_close (secondary_cipher);
  171. return NULL;
  172. }
  173. if (secondary_cipher->cipher->blocksize != GRUB_CRYPTODISK_GF_BYTES)
  174. {
  175. grub_crypto_cipher_close (cipher);
  176. grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported XTS block size: %d",
  177. secondary_cipher->cipher->blocksize);
  178. grub_crypto_cipher_close (secondary_cipher);
  179. return NULL;
  180. }
  181. }
  182. else if (grub_memcmp (ciphermode, "lrw-", sizeof ("lrw-") - 1) == 0)
  183. {
  184. mode = GRUB_CRYPTODISK_MODE_LRW;
  185. cipheriv = ciphermode + sizeof ("lrw-") - 1;
  186. if (cipher->cipher->blocksize != GRUB_CRYPTODISK_GF_BYTES)
  187. {
  188. grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported LRW block size: %d",
  189. cipher->cipher->blocksize);
  190. grub_crypto_cipher_close (cipher);
  191. return NULL;
  192. }
  193. }
  194. else
  195. {
  196. grub_crypto_cipher_close (cipher);
  197. grub_error (GRUB_ERR_BAD_ARGUMENT, "Unknown cipher mode: %s",
  198. ciphermode);
  199. return NULL;
  200. }
  201. if (cipheriv == NULL);
  202. else if (grub_memcmp (cipheriv, "plain", sizeof ("plain") - 1) == 0)
  203. mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
  204. else if (grub_memcmp (cipheriv, "plain64", sizeof ("plain64") - 1) == 0)
  205. mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN64;
  206. else if (grub_memcmp (cipheriv, "benbi", sizeof ("benbi") - 1) == 0)
  207. {
  208. if (cipher->cipher->blocksize & (cipher->cipher->blocksize - 1)
  209. || cipher->cipher->blocksize == 0)
  210. grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported benbi blocksize: %d",
  211. cipher->cipher->blocksize);
  212. /* FIXME should we return an error here? */
  213. for (benbi_log = 0;
  214. (cipher->cipher->blocksize << benbi_log) < GRUB_DISK_SECTOR_SIZE;
  215. benbi_log++);
  216. mode_iv = GRUB_CRYPTODISK_MODE_IV_BENBI;
  217. }
  218. else if (grub_memcmp (cipheriv, "null", sizeof ("null") - 1) == 0)
  219. mode_iv = GRUB_CRYPTODISK_MODE_IV_NULL;
  220. else if (grub_memcmp (cipheriv, "essiv:", sizeof ("essiv:") - 1) == 0)
  221. {
  222. char *hash_str = cipheriv + 6;
  223. mode_iv = GRUB_CRYPTODISK_MODE_IV_ESSIV;
  224. /* Configure the hash and cipher used for ESSIV. */
  225. essiv_hash = grub_crypto_lookup_md_by_name (hash_str);
  226. if (!essiv_hash)
  227. {
  228. grub_crypto_cipher_close (cipher);
  229. grub_crypto_cipher_close (secondary_cipher);
  230. grub_error (GRUB_ERR_FILE_NOT_FOUND,
  231. "Couldn't load %s hash", hash_str);
  232. return NULL;
  233. }
  234. essiv_cipher = grub_crypto_cipher_open (ciph);
  235. if (!essiv_cipher)
  236. {
  237. grub_crypto_cipher_close (cipher);
  238. grub_crypto_cipher_close (secondary_cipher);
  239. return NULL;
  240. }
  241. }
  242. else
  243. {
  244. grub_crypto_cipher_close (cipher);
  245. grub_crypto_cipher_close (secondary_cipher);
  246. grub_error (GRUB_ERR_BAD_ARGUMENT, "Unknown IV mode: %s",
  247. cipheriv);
  248. return NULL;
  249. }
  250. /* Configure the hash used for the AF splitter and HMAC. */
  251. hash = grub_crypto_lookup_md_by_name (hashspec);
  252. if (!hash)
  253. {
  254. grub_crypto_cipher_close (cipher);
  255. grub_crypto_cipher_close (essiv_cipher);
  256. grub_crypto_cipher_close (secondary_cipher);
  257. grub_error (GRUB_ERR_FILE_NOT_FOUND, "Couldn't load %s hash",
  258. hashspec);
  259. return NULL;
  260. }
  261. newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
  262. if (!newdev)
  263. {
  264. grub_crypto_cipher_close (cipher);
  265. grub_crypto_cipher_close (essiv_cipher);
  266. grub_crypto_cipher_close (secondary_cipher);
  267. return NULL;
  268. }
  269. newdev->cipher = cipher;
  270. newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
  271. newdev->source_disk = NULL;
  272. newdev->benbi_log = benbi_log;
  273. newdev->mode = mode;
  274. newdev->mode_iv = mode_iv;
  275. newdev->secondary_cipher = secondary_cipher;
  276. newdev->essiv_cipher = essiv_cipher;
  277. newdev->essiv_hash = essiv_hash;
  278. newdev->hash = hash;
  279. newdev->log_sector_size = 9;
  280. newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
  281. grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
  282. newdev->modname = "luks";
  283. COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= sizeof (uuid));
  284. return newdev;
  285. }
  286. static grub_err_t
  287. luks_recover_key (grub_disk_t source,
  288. grub_cryptodisk_t dev)
  289. {
  290. struct grub_luks_phdr header;
  291. grub_size_t keysize;
  292. grub_uint8_t *split_key = NULL;
  293. char passphrase[MAX_PASSPHRASE] = "";
  294. grub_uint8_t candidate_digest[sizeof (header.mkDigest)];
  295. unsigned i;
  296. grub_size_t length;
  297. grub_err_t err;
  298. grub_size_t max_stripes = 1;
  299. char *tmp;
  300. err = grub_disk_read (source, 0, 0, sizeof (header), &header);
  301. if (err)
  302. return err;
  303. grub_puts_ (N_("Attempting to decrypt master key..."));
  304. keysize = grub_be_to_cpu32 (header.keyBytes);
  305. if (keysize > GRUB_CRYPTODISK_MAX_KEYLEN)
  306. return grub_error (GRUB_ERR_BAD_FS, "key is too long");
  307. for (i = 0; i < ARRAY_SIZE (header.keyblock); i++)
  308. if (grub_be_to_cpu32 (header.keyblock[i].active) == LUKS_KEY_ENABLED
  309. && grub_be_to_cpu32 (header.keyblock[i].stripes) > max_stripes)
  310. max_stripes = grub_be_to_cpu32 (header.keyblock[i].stripes);
  311. split_key = grub_malloc (keysize * max_stripes);
  312. if (!split_key)
  313. return grub_errno;
  314. /* Get the passphrase from the user. */
  315. tmp = NULL;
  316. if (source->partition)
  317. tmp = grub_partition_get_name (source->partition);
  318. grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
  319. source->partition ? "," : "", tmp ? : "",
  320. dev->uuid);
  321. grub_free (tmp);
  322. if (!grub_password_get (passphrase, MAX_PASSPHRASE))
  323. {
  324. grub_free (split_key);
  325. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied");
  326. }
  327. /* Try to recover master key from each active keyslot. */
  328. for (i = 0; i < ARRAY_SIZE (header.keyblock); i++)
  329. {
  330. gcry_err_code_t gcry_err;
  331. grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
  332. grub_uint8_t digest[GRUB_CRYPTODISK_MAX_KEYLEN];
  333. /* Check if keyslot is enabled. */
  334. if (grub_be_to_cpu32 (header.keyblock[i].active) != LUKS_KEY_ENABLED)
  335. continue;
  336. grub_dprintf ("luks", "Trying keyslot %d\n", i);
  337. /* Calculate the PBKDF2 of the user supplied passphrase. */
  338. gcry_err = grub_crypto_pbkdf2 (dev->hash, (grub_uint8_t *) passphrase,
  339. grub_strlen (passphrase),
  340. header.keyblock[i].passwordSalt,
  341. sizeof (header.keyblock[i].passwordSalt),
  342. grub_be_to_cpu32 (header.keyblock[i].
  343. passwordIterations),
  344. digest, keysize);
  345. if (gcry_err)
  346. {
  347. grub_free (split_key);
  348. return grub_crypto_gcry_error (gcry_err);
  349. }
  350. grub_dprintf ("luks", "PBKDF2 done\n");
  351. gcry_err = grub_cryptodisk_setkey (dev, digest, keysize);
  352. if (gcry_err)
  353. {
  354. grub_free (split_key);
  355. return grub_crypto_gcry_error (gcry_err);
  356. }
  357. length = (keysize * grub_be_to_cpu32 (header.keyblock[i].stripes));
  358. /* Read and decrypt the key material from the disk. */
  359. err = grub_disk_read (source,
  360. grub_be_to_cpu32 (header.keyblock
  361. [i].keyMaterialOffset), 0,
  362. length, split_key);
  363. if (err)
  364. {
  365. grub_free (split_key);
  366. return err;
  367. }
  368. gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
  369. if (gcry_err)
  370. {
  371. grub_free (split_key);
  372. return grub_crypto_gcry_error (gcry_err);
  373. }
  374. /* Merge the decrypted key material to get the candidate master key. */
  375. gcry_err = AF_merge (dev->hash, split_key, candidate_key, keysize,
  376. grub_be_to_cpu32 (header.keyblock[i].stripes));
  377. if (gcry_err)
  378. {
  379. grub_free (split_key);
  380. return grub_crypto_gcry_error (gcry_err);
  381. }
  382. grub_dprintf ("luks", "candidate key recovered\n");
  383. /* Calculate the PBKDF2 of the candidate master key. */
  384. gcry_err = grub_crypto_pbkdf2 (dev->hash, candidate_key,
  385. grub_be_to_cpu32 (header.keyBytes),
  386. header.mkDigestSalt,
  387. sizeof (header.mkDigestSalt),
  388. grub_be_to_cpu32
  389. (header.mkDigestIterations),
  390. candidate_digest,
  391. sizeof (candidate_digest));
  392. if (gcry_err)
  393. {
  394. grub_free (split_key);
  395. return grub_crypto_gcry_error (gcry_err);
  396. }
  397. /* Compare the calculated PBKDF2 to the digest stored
  398. in the header to see if it's correct. */
  399. if (grub_memcmp (candidate_digest, header.mkDigest,
  400. sizeof (header.mkDigest)) != 0)
  401. {
  402. grub_dprintf ("luks", "bad digest\n");
  403. continue;
  404. }
  405. /* TRANSLATORS: It's a cryptographic key slot: one element of an array
  406. where each element is either empty or holds a key. */
  407. grub_printf_ (N_("Slot %d opened\n"), i);
  408. /* Set the master key. */
  409. gcry_err = grub_cryptodisk_setkey (dev, candidate_key, keysize);
  410. if (gcry_err)
  411. {
  412. grub_free (split_key);
  413. return grub_crypto_gcry_error (gcry_err);
  414. }
  415. grub_free (split_key);
  416. return GRUB_ERR_NONE;
  417. }
  418. grub_free (split_key);
  419. return GRUB_ACCESS_DENIED;
  420. }
  421. struct grub_cryptodisk_dev luks_crypto = {
  422. .scan = configure_ciphers,
  423. .recover_key = luks_recover_key
  424. };
  425. GRUB_MOD_INIT (luks)
  426. {
  427. COMPILE_TIME_ASSERT (sizeof (((struct grub_luks_phdr *) 0)->uuid)
  428. < GRUB_CRYPTODISK_MAX_UUID_LENGTH);
  429. grub_cryptodisk_dev_register (&luks_crypto);
  430. }
  431. GRUB_MOD_FINI (luks)
  432. {
  433. grub_cryptodisk_dev_unregister (&luks_crypto);
  434. }