luks2.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 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. #include <base64.h>
  29. #include <json.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. #define LUKS_MAGIC_1ST "LUKS\xBA\xBE"
  32. #define LUKS_MAGIC_2ND "SKUL\xBA\xBE"
  33. enum grub_luks2_kdf_type
  34. {
  35. LUKS2_KDF_TYPE_ARGON2I,
  36. LUKS2_KDF_TYPE_PBKDF2
  37. };
  38. typedef enum grub_luks2_kdf_type grub_luks2_kdf_type_t;
  39. /* On disk LUKS header */
  40. struct grub_luks2_header
  41. {
  42. char magic[6];
  43. grub_uint16_t version;
  44. grub_uint64_t hdr_size;
  45. grub_uint64_t seqid;
  46. char label[48];
  47. char csum_alg[32];
  48. grub_uint8_t salt[64];
  49. char uuid[40];
  50. char subsystem[48];
  51. grub_uint64_t hdr_offset;
  52. char _padding[184];
  53. grub_uint8_t csum[64];
  54. char _padding4096[7*512];
  55. } GRUB_PACKED;
  56. typedef struct grub_luks2_header grub_luks2_header_t;
  57. struct grub_luks2_keyslot
  58. {
  59. /* The integer key to the associative array of keyslots. */
  60. grub_uint64_t idx;
  61. grub_int64_t key_size;
  62. grub_int64_t priority;
  63. struct
  64. {
  65. const char *encryption;
  66. grub_uint64_t offset;
  67. grub_uint64_t size;
  68. grub_int64_t key_size;
  69. } area;
  70. struct
  71. {
  72. const char *hash;
  73. grub_int64_t stripes;
  74. } af;
  75. struct
  76. {
  77. grub_luks2_kdf_type_t type;
  78. const char *salt;
  79. union
  80. {
  81. struct
  82. {
  83. grub_int64_t time;
  84. grub_int64_t memory;
  85. grub_int64_t cpus;
  86. } argon2i;
  87. struct
  88. {
  89. const char *hash;
  90. grub_int64_t iterations;
  91. } pbkdf2;
  92. } u;
  93. } kdf;
  94. };
  95. typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
  96. struct grub_luks2_segment
  97. {
  98. grub_uint64_t idx;
  99. grub_uint64_t offset;
  100. const char *size;
  101. const char *encryption;
  102. grub_int64_t sector_size;
  103. };
  104. typedef struct grub_luks2_segment grub_luks2_segment_t;
  105. struct grub_luks2_digest
  106. {
  107. grub_uint64_t idx;
  108. /* Both keyslots and segments are interpreted as bitfields here */
  109. grub_uint64_t keyslots;
  110. grub_uint64_t segments;
  111. const char *salt;
  112. const char *digest;
  113. const char *hash;
  114. grub_int64_t iterations;
  115. };
  116. typedef struct grub_luks2_digest grub_luks2_digest_t;
  117. gcry_err_code_t AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src,
  118. grub_uint8_t * dst, grub_size_t blocksize,
  119. grub_size_t blocknumbers);
  120. static grub_err_t
  121. luks2_parse_keyslot (grub_luks2_keyslot_t *out, const grub_json_t *keyslot)
  122. {
  123. grub_json_t area, af, kdf;
  124. const char *type;
  125. if (grub_json_getstring (&type, keyslot, "type"))
  126. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing or invalid keyslot");
  127. else if (grub_strcmp (type, "luks2"))
  128. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported keyslot type %s", type);
  129. else if (grub_json_getint64 (&out->key_size, keyslot, "key_size"))
  130. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing keyslot information");
  131. if (grub_json_getint64 (&out->priority, keyslot, "priority"))
  132. out->priority = 1;
  133. if (grub_json_getvalue (&area, keyslot, "area") ||
  134. grub_json_getstring (&type, &area, "type"))
  135. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing or invalid key area");
  136. else if (grub_strcmp (type, "raw"))
  137. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported key area type: %s", type);
  138. else if (grub_json_getuint64 (&out->area.offset, &area, "offset") ||
  139. grub_json_getuint64 (&out->area.size, &area, "size") ||
  140. grub_json_getstring (&out->area.encryption, &area, "encryption") ||
  141. grub_json_getint64 (&out->area.key_size, &area, "key_size"))
  142. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing key area information");
  143. if (grub_json_getvalue (&kdf, keyslot, "kdf") ||
  144. grub_json_getstring (&type, &kdf, "type") ||
  145. grub_json_getstring (&out->kdf.salt, &kdf, "salt"))
  146. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing or invalid KDF");
  147. else if (!grub_strcmp (type, "argon2i") || !grub_strcmp (type, "argon2id"))
  148. {
  149. out->kdf.type = LUKS2_KDF_TYPE_ARGON2I;
  150. if (grub_json_getint64 (&out->kdf.u.argon2i.time, &kdf, "time") ||
  151. grub_json_getint64 (&out->kdf.u.argon2i.memory, &kdf, "memory") ||
  152. grub_json_getint64 (&out->kdf.u.argon2i.cpus, &kdf, "cpus"))
  153. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing Argon2i parameters");
  154. }
  155. else if (!grub_strcmp (type, "pbkdf2"))
  156. {
  157. out->kdf.type = LUKS2_KDF_TYPE_PBKDF2;
  158. if (grub_json_getstring (&out->kdf.u.pbkdf2.hash, &kdf, "hash") ||
  159. grub_json_getint64 (&out->kdf.u.pbkdf2.iterations, &kdf, "iterations"))
  160. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing PBKDF2 parameters");
  161. }
  162. else
  163. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported KDF type %s", type);
  164. if (grub_json_getvalue (&af, keyslot, "af") ||
  165. grub_json_getstring (&type, &af, "type"))
  166. return grub_error (GRUB_ERR_BAD_ARGUMENT, "missing or invalid area");
  167. if (grub_strcmp (type, "luks1"))
  168. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported AF type %s", type);
  169. if (grub_json_getint64 (&out->af.stripes, &af, "stripes") ||
  170. grub_json_getstring (&out->af.hash, &af, "hash"))
  171. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing AF parameters");
  172. return GRUB_ERR_NONE;
  173. }
  174. static grub_err_t
  175. luks2_parse_segment (grub_luks2_segment_t *out, const grub_json_t *segment)
  176. {
  177. const char *type;
  178. if (grub_json_getstring (&type, segment, "type"))
  179. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid segment type");
  180. else if (grub_strcmp (type, "crypt"))
  181. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported segment type %s", type);
  182. if (grub_json_getuint64 (&out->offset, segment, "offset") ||
  183. grub_json_getstring (&out->size, segment, "size") ||
  184. grub_json_getstring (&out->encryption, segment, "encryption") ||
  185. grub_json_getint64 (&out->sector_size, segment, "sector_size"))
  186. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing segment parameters");
  187. return GRUB_ERR_NONE;
  188. }
  189. static grub_err_t
  190. luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
  191. {
  192. grub_json_t segments, keyslots, o;
  193. grub_size_t i, size;
  194. grub_uint64_t bit;
  195. const char *type;
  196. if (grub_json_getstring (&type, digest, "type"))
  197. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid digest type");
  198. else if (grub_strcmp (type, "pbkdf2"))
  199. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Unsupported digest type %s", type);
  200. if (grub_json_getvalue (&segments, digest, "segments") ||
  201. grub_json_getvalue (&keyslots, digest, "keyslots") ||
  202. grub_json_getstring (&out->salt, digest, "salt") ||
  203. grub_json_getstring (&out->digest, digest, "digest") ||
  204. grub_json_getstring (&out->hash, digest, "hash") ||
  205. grub_json_getint64 (&out->iterations, digest, "iterations"))
  206. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing digest parameters");
  207. if (grub_json_getsize (&size, &segments))
  208. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  209. "Digest references no segments");
  210. out->segments = 0;
  211. for (i = 0; i < size; i++)
  212. {
  213. if (grub_json_getchild (&o, &segments, i) ||
  214. grub_json_getuint64 (&bit, &o, NULL))
  215. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid segment");
  216. out->segments |= (1 << bit);
  217. }
  218. if (grub_json_getsize (&size, &keyslots))
  219. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  220. "Digest references no keyslots");
  221. out->keyslots = 0;
  222. for (i = 0; i < size; i++)
  223. {
  224. if (grub_json_getchild (&o, &keyslots, i) ||
  225. grub_json_getuint64 (&bit, &o, NULL))
  226. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid keyslot");
  227. out->keyslots |= (1 << bit);
  228. }
  229. return GRUB_ERR_NONE;
  230. }
  231. static grub_err_t
  232. luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
  233. const grub_json_t *root, grub_size_t keyslot_json_idx)
  234. {
  235. grub_json_t keyslots, keyslot, digests, digest, segments, segment;
  236. grub_size_t json_idx, size;
  237. /* Get nth keyslot */
  238. if (grub_json_getvalue (&keyslots, root, "keyslots") ||
  239. grub_json_getchild (&keyslot, &keyslots, keyslot_json_idx) ||
  240. grub_json_getuint64 (&k->idx, &keyslot, NULL) ||
  241. grub_json_getchild (&keyslot, &keyslot, 0) ||
  242. luks2_parse_keyslot (k, &keyslot))
  243. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %" PRIuGRUB_SIZE, keyslot_json_idx);
  244. /* Get digest that matches the keyslot. */
  245. if (grub_json_getvalue (&digests, root, "digests") ||
  246. grub_json_getsize (&size, &digests))
  247. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
  248. for (json_idx = 0; json_idx < size; json_idx++)
  249. {
  250. if (grub_json_getchild (&digest, &digests, json_idx) ||
  251. grub_json_getuint64 (&d->idx, &digest, NULL) ||
  252. grub_json_getchild (&digest, &digest, 0) ||
  253. luks2_parse_digest (d, &digest))
  254. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %" PRIuGRUB_SIZE, json_idx);
  255. if ((d->keyslots & (1 << k->idx)))
  256. break;
  257. }
  258. if (json_idx == size)
  259. return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%" PRIuGRUB_UINT64_T "\"", k->idx);
  260. /* Get segment that matches the digest. */
  261. if (grub_json_getvalue (&segments, root, "segments") ||
  262. grub_json_getsize (&size, &segments))
  263. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
  264. for (json_idx = 0; json_idx < size; json_idx++)
  265. {
  266. if (grub_json_getchild (&segment, &segments, json_idx) ||
  267. grub_json_getuint64 (&s->idx, &segment, NULL) ||
  268. grub_json_getchild (&segment, &segment, 0) ||
  269. luks2_parse_segment (s, &segment))
  270. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %" PRIuGRUB_SIZE, json_idx);
  271. if ((d->segments & (1 << s->idx)))
  272. break;
  273. }
  274. if (json_idx == size)
  275. return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%" PRIuGRUB_UINT64_T "\"", d->idx);
  276. return GRUB_ERR_NONE;
  277. }
  278. /* Determine whether to use primary or secondary header */
  279. static grub_err_t
  280. luks2_read_header (grub_disk_t disk, grub_luks2_header_t *outhdr)
  281. {
  282. grub_luks2_header_t primary, secondary, *header = &primary;
  283. grub_err_t ret;
  284. /* Read the primary LUKS header. */
  285. ret = grub_disk_read (disk, 0, 0, sizeof (primary), &primary);
  286. if (ret)
  287. return ret;
  288. /* Look for LUKS magic sequence. */
  289. if (grub_memcmp (primary.magic, LUKS_MAGIC_1ST, sizeof (primary.magic)) ||
  290. grub_be_to_cpu16 (primary.version) != 2)
  291. return GRUB_ERR_BAD_SIGNATURE;
  292. /* Read the secondary header. */
  293. ret = grub_disk_read (disk, 0, grub_be_to_cpu64 (primary.hdr_size), sizeof (secondary), &secondary);
  294. if (ret)
  295. return ret;
  296. /* Look for LUKS magic sequence. */
  297. if (grub_memcmp (secondary.magic, LUKS_MAGIC_2ND, sizeof (secondary.magic)) ||
  298. grub_be_to_cpu16 (secondary.version) != 2)
  299. return GRUB_ERR_BAD_SIGNATURE;
  300. if (grub_be_to_cpu64 (primary.seqid) < grub_be_to_cpu64 (secondary.seqid))
  301. header = &secondary;
  302. grub_memcpy (outhdr, header, sizeof (*header));
  303. return GRUB_ERR_NONE;
  304. }
  305. static grub_cryptodisk_t
  306. luks2_scan (grub_disk_t disk, grub_cryptomount_args_t cargs)
  307. {
  308. grub_cryptodisk_t cryptodisk;
  309. grub_luks2_header_t header;
  310. if (cargs->check_boot)
  311. return NULL;
  312. if (luks2_read_header (disk, &header))
  313. {
  314. grub_errno = GRUB_ERR_NONE;
  315. return NULL;
  316. }
  317. if (cargs->search_uuid != NULL && grub_uuidcasecmp (cargs->search_uuid, header.uuid, sizeof (header.uuid)) != 0)
  318. {
  319. grub_dprintf ("luks2", "%s != %s\n", header.uuid, cargs->search_uuid);
  320. return NULL;
  321. }
  322. cryptodisk = grub_zalloc (sizeof (*cryptodisk));
  323. if (!cryptodisk)
  324. return NULL;
  325. COMPILE_TIME_ASSERT (sizeof (cryptodisk->uuid) >= sizeof (header.uuid));
  326. grub_memcpy (cryptodisk->uuid, header.uuid, sizeof (header.uuid));
  327. cryptodisk->modname = "luks2";
  328. return cryptodisk;
  329. }
  330. static grub_err_t
  331. luks2_base64_decode (const char *in, grub_size_t inlen, grub_uint8_t *decoded, idx_t *decodedlen)
  332. {
  333. grub_size_t unescaped_len = 0;
  334. char *unescaped = NULL;
  335. bool successful;
  336. if (grub_json_unescape (&unescaped, &unescaped_len, in, inlen) != GRUB_ERR_NONE)
  337. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("could not unescape Base64 string"));
  338. successful = base64_decode (unescaped, unescaped_len, (char *) decoded, decodedlen);
  339. grub_free (unescaped);
  340. if (!successful)
  341. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("could not decode Base64 string"));
  342. return GRUB_ERR_NONE;
  343. }
  344. static grub_err_t
  345. luks2_verify_key (grub_luks2_digest_t *d, grub_uint8_t *candidate_key,
  346. grub_size_t candidate_key_len)
  347. {
  348. grub_uint8_t candidate_digest[GRUB_CRYPTODISK_MAX_KEYLEN];
  349. grub_uint8_t digest[GRUB_CRYPTODISK_MAX_KEYLEN], salt[GRUB_CRYPTODISK_MAX_KEYLEN];
  350. idx_t saltlen = sizeof (salt), digestlen = sizeof (digest);
  351. const gcry_md_spec_t *hash;
  352. gcry_err_code_t gcry_ret;
  353. /* Decode both digest and salt */
  354. if (luks2_base64_decode (d->digest, grub_strlen (d->digest),
  355. digest, &digestlen) != GRUB_ERR_NONE)
  356. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid digest");
  357. if (luks2_base64_decode (d->salt, grub_strlen (d->salt),
  358. salt, &saltlen) != GRUB_ERR_NONE)
  359. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid digest salt");
  360. /* Configure the hash used for the digest. */
  361. hash = grub_crypto_lookup_md_by_name (d->hash);
  362. if (!hash)
  363. return grub_error (GRUB_ERR_FILE_NOT_FOUND, "Couldn't load %s hash", d->hash);
  364. /* Calculate the candidate key's digest */
  365. gcry_ret = grub_crypto_pbkdf2 (hash,
  366. candidate_key, candidate_key_len,
  367. salt, saltlen,
  368. d->iterations,
  369. candidate_digest, digestlen);
  370. if (gcry_ret)
  371. return grub_crypto_gcry_error (gcry_ret);
  372. if (grub_memcmp (candidate_digest, digest, digestlen) != 0)
  373. return grub_error (GRUB_ERR_ACCESS_DENIED, "Mismatching digests");
  374. return GRUB_ERR_NONE;
  375. }
  376. static grub_err_t
  377. luks2_decrypt_key (grub_uint8_t *out_key,
  378. grub_disk_t source, grub_cryptodisk_t crypt,
  379. grub_luks2_keyslot_t *k,
  380. const grub_uint8_t *passphrase, grub_size_t passphraselen)
  381. {
  382. grub_uint8_t area_key[GRUB_CRYPTODISK_MAX_KEYLEN];
  383. grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
  384. grub_uint8_t *split_key = NULL;
  385. idx_t saltlen = sizeof (salt);
  386. char cipher[32], *p;
  387. const gcry_md_spec_t *hash;
  388. gcry_err_code_t gcry_ret;
  389. grub_err_t ret;
  390. if (luks2_base64_decode (k->kdf.salt, grub_strlen (k->kdf.salt),
  391. salt, &saltlen) != GRUB_ERR_NONE)
  392. {
  393. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid keyslot salt");
  394. goto err;
  395. }
  396. /* Calculate the binary area key of the user supplied passphrase. */
  397. switch (k->kdf.type)
  398. {
  399. case LUKS2_KDF_TYPE_ARGON2I:
  400. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Argon2 not supported");
  401. goto err;
  402. case LUKS2_KDF_TYPE_PBKDF2:
  403. hash = grub_crypto_lookup_md_by_name (k->kdf.u.pbkdf2.hash);
  404. if (!hash)
  405. {
  406. ret = grub_error (GRUB_ERR_FILE_NOT_FOUND, "Couldn't load %s hash",
  407. k->kdf.u.pbkdf2.hash);
  408. goto err;
  409. }
  410. gcry_ret = grub_crypto_pbkdf2 (hash, (grub_uint8_t *) passphrase,
  411. passphraselen,
  412. salt, saltlen,
  413. k->kdf.u.pbkdf2.iterations,
  414. area_key, k->area.key_size);
  415. if (gcry_ret)
  416. {
  417. ret = grub_crypto_gcry_error (gcry_ret);
  418. goto err;
  419. }
  420. break;
  421. }
  422. /* Set up disk encryption parameters for the key area */
  423. grub_strncpy (cipher, k->area.encryption, sizeof (cipher));
  424. p = grub_memchr (cipher, '-', grub_strlen (cipher));
  425. if (!p)
  426. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid encryption");
  427. *p = '\0';
  428. ret = grub_cryptodisk_setcipher (crypt, cipher, p + 1);
  429. if (ret)
  430. return ret;
  431. gcry_ret = grub_cryptodisk_setkey (crypt, area_key, k->area.key_size);
  432. if (gcry_ret)
  433. {
  434. ret = grub_crypto_gcry_error (gcry_ret);
  435. goto err;
  436. }
  437. /* Read and decrypt the binary key area with the area key. */
  438. split_key = grub_malloc (k->area.size);
  439. if (!split_key)
  440. {
  441. ret = grub_errno;
  442. goto err;
  443. }
  444. grub_errno = GRUB_ERR_NONE;
  445. ret = grub_disk_read (source, 0, k->area.offset, k->area.size, split_key);
  446. if (ret)
  447. {
  448. grub_error (GRUB_ERR_IO, "Read error: %s\n", grub_errmsg);
  449. goto err;
  450. }
  451. /*
  452. * The key slots area is always encrypted in 512-byte sectors,
  453. * regardless of encrypted data sector size.
  454. */
  455. gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
  456. GRUB_LUKS1_LOG_SECTOR_SIZE);
  457. if (gcry_ret)
  458. {
  459. ret = grub_crypto_gcry_error (gcry_ret);
  460. goto err;
  461. }
  462. /* Configure the hash used for anti-forensic merging. */
  463. hash = grub_crypto_lookup_md_by_name (k->af.hash);
  464. if (!hash)
  465. {
  466. ret = grub_error (GRUB_ERR_FILE_NOT_FOUND, "Couldn't load %s hash",
  467. k->af.hash);
  468. goto err;
  469. }
  470. /* Merge the decrypted key material to get the candidate master key. */
  471. gcry_ret = AF_merge (hash, split_key, out_key, k->key_size, k->af.stripes);
  472. if (gcry_ret)
  473. {
  474. ret = grub_crypto_gcry_error (gcry_ret);
  475. goto err;
  476. }
  477. grub_dprintf ("luks2", "Candidate key recovered\n");
  478. err:
  479. grub_free (split_key);
  480. return ret;
  481. }
  482. static grub_err_t
  483. luks2_recover_key (grub_disk_t source,
  484. grub_cryptodisk_t crypt,
  485. grub_cryptomount_args_t cargs)
  486. {
  487. grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
  488. char cipher[32], *json_header = NULL, *ptr;
  489. grub_size_t candidate_key_len = 0, json_idx, size;
  490. grub_luks2_header_t header;
  491. grub_luks2_keyslot_t keyslot;
  492. grub_luks2_digest_t digest;
  493. grub_luks2_segment_t segment;
  494. gcry_err_code_t gcry_ret;
  495. grub_json_t *json = NULL, keyslots;
  496. grub_err_t ret;
  497. if (cargs->key_data == NULL || cargs->key_len == 0)
  498. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no key data");
  499. ret = luks2_read_header (source, &header);
  500. if (ret)
  501. return ret;
  502. json_header = grub_zalloc (grub_be_to_cpu64 (header.hdr_size) - sizeof (header));
  503. if (!json_header)
  504. return GRUB_ERR_OUT_OF_MEMORY;
  505. /* Read the JSON area. */
  506. ret = grub_disk_read (source, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
  507. grub_be_to_cpu64 (header.hdr_size) - sizeof (header), json_header);
  508. if (ret)
  509. goto err;
  510. ptr = grub_memchr (json_header, 0, grub_be_to_cpu64 (header.hdr_size) - sizeof (header));
  511. if (!ptr)
  512. goto err;
  513. ret = grub_json_parse (&json, json_header, grub_be_to_cpu64 (header.hdr_size));
  514. if (ret)
  515. {
  516. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid LUKS2 JSON header");
  517. goto err;
  518. }
  519. if (grub_json_getvalue (&keyslots, json, "keyslots") ||
  520. grub_json_getsize (&size, &keyslots))
  521. {
  522. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get keyslots");
  523. goto err;
  524. }
  525. if (grub_disk_native_sectors (source) == GRUB_DISK_SIZE_UNKNOWN)
  526. {
  527. /* FIXME: Allow use of source disk, and maybe cause errors in read. */
  528. grub_dprintf ("luks2", "Source disk %s has an unknown size, "
  529. "conservatively returning error\n", source->name);
  530. ret = grub_error (GRUB_ERR_BUG, "Unknown size of luks2 source device");
  531. goto err;
  532. }
  533. /* Try all keyslot */
  534. for (json_idx = 0; json_idx < size; json_idx++)
  535. {
  536. char indexstr[21]; /* log10(2^64) ~ 20, plus NUL character. */
  537. typeof (source->total_sectors) max_crypt_sectors = 0;
  538. grub_errno = GRUB_ERR_NONE;
  539. ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, json_idx);
  540. if (ret)
  541. {
  542. /*
  543. * luks2_get_keyslot() can fail for a variety of reasons that do not
  544. * necessarily mean the next keyslot should not be tried (e.g. a new
  545. * kdf type). So always try the next slot.
  546. */
  547. grub_dprintf ("luks2", "Failed to get keyslot %" PRIuGRUB_UINT64_T "\n", keyslot.idx);
  548. continue;
  549. }
  550. if (grub_errno != GRUB_ERR_NONE)
  551. grub_dprintf ("luks2", "Ignoring unhandled error %d from luks2_get_keyslot\n", grub_errno);
  552. if (keyslot.priority == 0)
  553. {
  554. grub_dprintf ("luks2", "Ignoring keyslot \"%" PRIuGRUB_UINT64_T "\" due to priority\n", keyslot.idx);
  555. continue;
  556. }
  557. grub_dprintf ("luks2", "Trying keyslot \"%" PRIuGRUB_UINT64_T "\"\n", keyslot.idx);
  558. /* Sector size should be one of 512, 1024, 2048, or 4096. */
  559. if (!(segment.sector_size == 512 || segment.sector_size == 1024 ||
  560. segment.sector_size == 2048 || segment.sector_size == 4096))
  561. {
  562. grub_dprintf ("luks2", "Segment \"%" PRIuGRUB_UINT64_T "\" sector"
  563. " size %" PRIuGRUB_UINT64_T " is not one of"
  564. " 512, 1024, 2048, or 4096\n",
  565. segment.idx, segment.sector_size);
  566. continue;
  567. }
  568. /* Set up disk according to keyslot's segment. */
  569. crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
  570. crypt->log_sector_size = grub_log2ull (segment.sector_size);
  571. /* Set to the source disk/partition size, which is the maximum we allow. */
  572. max_crypt_sectors = grub_disk_native_sectors (source);
  573. max_crypt_sectors = grub_convert_sector (max_crypt_sectors, GRUB_DISK_SECTOR_BITS,
  574. crypt->log_sector_size);
  575. if (max_crypt_sectors < crypt->offset_sectors)
  576. {
  577. grub_dprintf ("luks2", "Segment \"%" PRIuGRUB_UINT64_T "\" has offset"
  578. " %" PRIuGRUB_UINT64_T " which is greater than"
  579. " source disk size %" PRIuGRUB_UINT64_T ","
  580. " skipping\n", segment.idx, crypt->offset_sectors,
  581. max_crypt_sectors);
  582. continue;
  583. }
  584. if (grub_strcmp (segment.size, "dynamic") == 0)
  585. crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors;
  586. else
  587. {
  588. grub_errno = GRUB_ERR_NONE;
  589. /* Convert segment.size to sectors, rounding up to nearest sector */
  590. crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
  591. if (grub_errno == GRUB_ERR_NONE)
  592. {
  593. crypt->total_sectors = ALIGN_UP (crypt->total_sectors,
  594. 1 << crypt->log_sector_size);
  595. crypt->total_sectors >>= crypt->log_sector_size;
  596. }
  597. else if (grub_errno == GRUB_ERR_BAD_NUMBER)
  598. {
  599. grub_dprintf ("luks2", "Segment \"%" PRIuGRUB_UINT64_T "\" size"
  600. " \"%s\" is not a parsable number,"
  601. " skipping keyslot\n",
  602. segment.idx, segment.size);
  603. continue;
  604. }
  605. else if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  606. {
  607. /*
  608. * There was an overflow in parsing segment.size, so disk must
  609. * be very large or the string is incorrect.
  610. *
  611. * TODO: Allow reading of at least up max_crypt_sectors. Really,
  612. * its very unlikely one would be booting from such a large drive
  613. * anyway. Use another smaller LUKS2 boot device.
  614. */
  615. grub_dprintf ("luks2", "Segment \"%" PRIuGRUB_UINT64_T "\" size"
  616. " %s overflowed 64-bit unsigned integer,"
  617. " skipping keyslot\n", segment.idx, segment.size);
  618. continue;
  619. }
  620. }
  621. if (crypt->total_sectors == 0)
  622. {
  623. grub_dprintf ("luks2", "Segment \"%" PRIuGRUB_UINT64_T "\" has zero"
  624. " sectors, skipping\n", segment.idx);
  625. continue;
  626. }
  627. else if (max_crypt_sectors < (crypt->offset_sectors + crypt->total_sectors))
  628. {
  629. grub_dprintf ("luks2", "Segment \"%" PRIuGRUB_UINT64_T "\" has last"
  630. " data position greater than source disk size,"
  631. " the end of the crypto device will be"
  632. " inaccessible\n", segment.idx);
  633. /* Allow decryption up to the end of the source disk. */
  634. crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors;
  635. }
  636. ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
  637. cargs->key_data, cargs->key_len);
  638. if (ret)
  639. {
  640. grub_dprintf ("luks2", "Decryption with keyslot \"%" PRIuGRUB_UINT64_T "\" failed: %s\n",
  641. keyslot.idx, grub_errmsg);
  642. continue;
  643. }
  644. ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
  645. if (ret)
  646. {
  647. grub_dprintf ("luks2", "Could not open keyslot \"%" PRIuGRUB_UINT64_T "\": %s\n",
  648. keyslot.idx, grub_errmsg);
  649. continue;
  650. }
  651. grub_snprintf (indexstr, sizeof (indexstr) - 1, "%" PRIuGRUB_UINT64_T, keyslot.idx);
  652. /*
  653. * TRANSLATORS: It's a cryptographic key slot: one element of an array
  654. * where each element is either empty or holds a key.
  655. */
  656. grub_printf_ (N_("Slot \"%s\" opened\n"), indexstr);
  657. candidate_key_len = keyslot.key_size;
  658. break;
  659. }
  660. if (candidate_key_len == 0)
  661. {
  662. ret = grub_error (GRUB_ERR_ACCESS_DENIED, "Invalid passphrase");
  663. goto err;
  664. }
  665. /* Set up disk cipher. */
  666. grub_strncpy (cipher, segment.encryption, sizeof (cipher));
  667. ptr = grub_memchr (cipher, '-', grub_strlen (cipher));
  668. if (!ptr)
  669. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid encryption");
  670. *ptr = '\0';
  671. ret = grub_cryptodisk_setcipher (crypt, cipher, ptr + 1);
  672. if (ret)
  673. goto err;
  674. /* Set the master key. */
  675. gcry_ret = grub_cryptodisk_setkey (crypt, candidate_key, candidate_key_len);
  676. if (gcry_ret)
  677. {
  678. ret = grub_crypto_gcry_error (gcry_ret);
  679. goto err;
  680. }
  681. err:
  682. grub_free (json_header);
  683. grub_json_free (json);
  684. return ret;
  685. }
  686. static struct grub_cryptodisk_dev luks2_crypto = {
  687. .scan = luks2_scan,
  688. .recover_key = luks2_recover_key
  689. };
  690. GRUB_MOD_INIT (luks2)
  691. {
  692. grub_cryptodisk_dev_register (&luks2_crypto);
  693. }
  694. GRUB_MOD_FINI (luks2)
  695. {
  696. grub_cryptodisk_dev_unregister (&luks2_crypto);
  697. }