cryptodisk.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  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/env.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/dl.h>
  23. #include <grub/extcmd.h>
  24. #include <grub/i18n.h>
  25. #include <grub/fs.h>
  26. #include <grub/file.h>
  27. #include <grub/procfs.h>
  28. #include <grub/partition.h>
  29. #ifdef GRUB_UTIL
  30. #include <grub/emu/hostdisk.h>
  31. #endif
  32. GRUB_MOD_LICENSE ("GPLv3+");
  33. grub_cryptodisk_dev_t grub_cryptodisk_list;
  34. enum
  35. {
  36. OPTION_UUID,
  37. OPTION_ALL,
  38. OPTION_BOOT,
  39. OPTION_PASSWORD,
  40. OPTION_KEYFILE,
  41. OPTION_KEYFILE_OFFSET,
  42. OPTION_KEYFILE_SIZE,
  43. OPTION_HEADER
  44. };
  45. static const struct grub_arg_option options[] =
  46. {
  47. {"uuid", 'u', 0, N_("Mount by UUID."), 0, 0},
  48. /* TRANSLATORS: It's still restricted to cryptodisks only. */
  49. {"all", 'a', 0, N_("Mount all."), 0, 0},
  50. {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0},
  51. {"password", 'p', 0, N_("Password to open volumes."), 0, ARG_TYPE_STRING},
  52. {"key-file", 'k', 0, N_("Key file"), 0, ARG_TYPE_STRING},
  53. {"keyfile-offset", 'O', 0, N_("Key file offset (bytes)"), 0, ARG_TYPE_INT},
  54. {"keyfile-size", 'S', 0, N_("Key file data size (bytes)"), 0, ARG_TYPE_INT},
  55. {"header", 'H', 0, N_("Read header from file"), 0, ARG_TYPE_STRING},
  56. {0, 0, 0, 0, 0, 0}
  57. };
  58. struct cryptodisk_read_hook_ctx
  59. {
  60. grub_file_t hdr_file;
  61. grub_disk_addr_t part_start;
  62. };
  63. typedef struct cryptodisk_read_hook_ctx *cryptodisk_read_hook_ctx_t;
  64. /* Our irreducible polynom is x^128+x^7+x^2+x+1. Lowest byte of it is: */
  65. #define GF_POLYNOM 0x87
  66. static inline int GF_PER_SECTOR (const struct grub_cryptodisk *dev)
  67. {
  68. return 1U << (dev->log_sector_size - GRUB_CRYPTODISK_GF_LOG_BYTES);
  69. }
  70. static grub_cryptodisk_t cryptodisk_list = NULL;
  71. static grub_uint8_t last_cryptodisk_id = 0;
  72. static void
  73. gf_mul_x (grub_uint8_t *g)
  74. {
  75. int over = 0, over2 = 0;
  76. unsigned j;
  77. for (j = 0; j < GRUB_CRYPTODISK_GF_BYTES; j++)
  78. {
  79. over2 = !!(g[j] & 0x80);
  80. g[j] <<= 1;
  81. g[j] |= over;
  82. over = over2;
  83. }
  84. if (over)
  85. g[0] ^= GF_POLYNOM;
  86. }
  87. static void
  88. gf_mul_x_be (grub_uint8_t *g)
  89. {
  90. int over = 0, over2 = 0;
  91. int j;
  92. for (j = (int) GRUB_CRYPTODISK_GF_BYTES - 1; j >= 0; j--)
  93. {
  94. over2 = !!(g[j] & 0x80);
  95. g[j] <<= 1;
  96. g[j] |= over;
  97. over = over2;
  98. }
  99. if (over)
  100. g[GRUB_CRYPTODISK_GF_BYTES - 1] ^= GF_POLYNOM;
  101. }
  102. static void
  103. gf_mul_be (grub_uint8_t *o, const grub_uint8_t *a, const grub_uint8_t *b)
  104. {
  105. unsigned i;
  106. grub_uint8_t t[GRUB_CRYPTODISK_GF_BYTES];
  107. grub_memset (o, 0, GRUB_CRYPTODISK_GF_BYTES);
  108. grub_memcpy (t, b, GRUB_CRYPTODISK_GF_BYTES);
  109. for (i = 0; i < GRUB_CRYPTODISK_GF_SIZE; i++)
  110. {
  111. if (((a[GRUB_CRYPTODISK_GF_BYTES - i / GRUB_CHAR_BIT - 1] >> (i % GRUB_CHAR_BIT))) & 1)
  112. grub_crypto_xor (o, o, t, GRUB_CRYPTODISK_GF_BYTES);
  113. gf_mul_x_be (t);
  114. }
  115. }
  116. static gcry_err_code_t
  117. grub_crypto_pcbc_decrypt (grub_crypto_cipher_handle_t cipher,
  118. void *out, void *in, grub_size_t size,
  119. void *iv)
  120. {
  121. grub_uint8_t *inptr, *outptr, *end;
  122. grub_uint8_t ivt[GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE];
  123. grub_size_t blocksize;
  124. if (!cipher->cipher->decrypt)
  125. return GPG_ERR_NOT_SUPPORTED;
  126. blocksize = cipher->cipher->blocksize;
  127. if (blocksize == 0 || (((blocksize - 1) & blocksize) != 0)
  128. || ((size & (blocksize - 1)) != 0))
  129. return GPG_ERR_INV_ARG;
  130. if (blocksize > GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE)
  131. return GPG_ERR_INV_ARG;
  132. end = (grub_uint8_t *) in + size;
  133. for (inptr = in, outptr = out; inptr < end;
  134. inptr += blocksize, outptr += blocksize)
  135. {
  136. grub_memcpy (ivt, inptr, blocksize);
  137. cipher->cipher->decrypt (cipher->ctx, outptr, inptr);
  138. grub_crypto_xor (outptr, outptr, iv, blocksize);
  139. grub_crypto_xor (iv, ivt, outptr, blocksize);
  140. }
  141. return GPG_ERR_NO_ERROR;
  142. }
  143. static gcry_err_code_t
  144. grub_crypto_pcbc_encrypt (grub_crypto_cipher_handle_t cipher,
  145. void *out, void *in, grub_size_t size,
  146. void *iv)
  147. {
  148. grub_uint8_t *inptr, *outptr, *end;
  149. grub_uint8_t ivt[GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE];
  150. grub_size_t blocksize;
  151. if (!cipher->cipher->encrypt)
  152. return GPG_ERR_NOT_SUPPORTED;
  153. blocksize = cipher->cipher->blocksize;
  154. if (blocksize > GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE)
  155. return GPG_ERR_INV_ARG;
  156. if (blocksize == 0 || (((blocksize - 1) & blocksize) != 0)
  157. || ((size & (blocksize - 1)) != 0))
  158. return GPG_ERR_INV_ARG;
  159. end = (grub_uint8_t *) in + size;
  160. for (inptr = in, outptr = out; inptr < end;
  161. inptr += blocksize, outptr += blocksize)
  162. {
  163. grub_memcpy (ivt, inptr, blocksize);
  164. grub_crypto_xor (outptr, outptr, iv, blocksize);
  165. cipher->cipher->encrypt (cipher->ctx, outptr, inptr);
  166. grub_crypto_xor (iv, ivt, outptr, blocksize);
  167. }
  168. return GPG_ERR_NO_ERROR;
  169. }
  170. struct lrw_sector
  171. {
  172. grub_uint8_t low[GRUB_CRYPTODISK_GF_BYTES];
  173. grub_uint8_t high[GRUB_CRYPTODISK_GF_BYTES];
  174. grub_uint8_t low_byte, low_byte_c;
  175. };
  176. static void
  177. generate_lrw_sector (struct lrw_sector *sec,
  178. const struct grub_cryptodisk *dev,
  179. const grub_uint8_t *iv)
  180. {
  181. grub_uint8_t idx[GRUB_CRYPTODISK_GF_BYTES];
  182. grub_uint16_t c;
  183. int j;
  184. grub_memcpy (idx, iv, GRUB_CRYPTODISK_GF_BYTES);
  185. sec->low_byte = (idx[GRUB_CRYPTODISK_GF_BYTES - 1]
  186. & (GF_PER_SECTOR (dev) - 1));
  187. sec->low_byte_c = (((GF_PER_SECTOR (dev) - 1) & ~sec->low_byte) + 1);
  188. idx[GRUB_CRYPTODISK_GF_BYTES - 1] &= ~(GF_PER_SECTOR (dev) - 1);
  189. gf_mul_be (sec->low, dev->lrw_key, idx);
  190. if (!sec->low_byte)
  191. return;
  192. c = idx[GRUB_CRYPTODISK_GF_BYTES - 1] + GF_PER_SECTOR (dev);
  193. if (c & 0x100)
  194. {
  195. for (j = GRUB_CRYPTODISK_GF_BYTES - 2; j >= 0; j--)
  196. {
  197. idx[j]++;
  198. if (idx[j] != 0)
  199. break;
  200. }
  201. }
  202. idx[GRUB_CRYPTODISK_GF_BYTES - 1] = c;
  203. gf_mul_be (sec->high, dev->lrw_key, idx);
  204. }
  205. static void __attribute__ ((unused))
  206. lrw_xor (const struct lrw_sector *sec,
  207. const struct grub_cryptodisk *dev,
  208. grub_uint8_t *b)
  209. {
  210. unsigned i;
  211. for (i = 0; i < sec->low_byte_c * GRUB_CRYPTODISK_GF_BYTES;
  212. i += GRUB_CRYPTODISK_GF_BYTES)
  213. grub_crypto_xor (b + i, b + i, sec->low, GRUB_CRYPTODISK_GF_BYTES);
  214. grub_crypto_xor (b, b, dev->lrw_precalc + GRUB_CRYPTODISK_GF_BYTES * sec->low_byte,
  215. sec->low_byte_c * GRUB_CRYPTODISK_GF_BYTES);
  216. if (!sec->low_byte)
  217. return;
  218. for (i = sec->low_byte_c * GRUB_CRYPTODISK_GF_BYTES;
  219. i < (1U << dev->log_sector_size); i += GRUB_CRYPTODISK_GF_BYTES)
  220. grub_crypto_xor (b + i, b + i, sec->high, GRUB_CRYPTODISK_GF_BYTES);
  221. grub_crypto_xor (b + sec->low_byte_c * GRUB_CRYPTODISK_GF_BYTES,
  222. b + sec->low_byte_c * GRUB_CRYPTODISK_GF_BYTES,
  223. dev->lrw_precalc, sec->low_byte * GRUB_CRYPTODISK_GF_BYTES);
  224. }
  225. static gcry_err_code_t
  226. grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
  227. grub_uint8_t * data, grub_size_t len,
  228. grub_disk_addr_t sector, grub_size_t log_sector_size,
  229. int do_encrypt)
  230. {
  231. grub_size_t i;
  232. gcry_err_code_t err;
  233. if (dev->cipher->cipher->blocksize > GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE)
  234. return GPG_ERR_INV_ARG;
  235. /* The only mode without IV. */
  236. if (dev->mode == GRUB_CRYPTODISK_MODE_ECB && !dev->rekey)
  237. return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
  238. : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
  239. for (i = 0; i < len; i += ((grub_size_t) 1 << log_sector_size))
  240. {
  241. grub_size_t sz = ((dev->cipher->cipher->blocksize
  242. + sizeof (grub_uint32_t) - 1)
  243. / sizeof (grub_uint32_t));
  244. grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4];
  245. if (dev->rekey)
  246. {
  247. grub_uint64_t zone = sector >> dev->rekey_shift;
  248. if (zone != dev->last_rekey)
  249. {
  250. err = dev->rekey (dev, zone);
  251. if (err)
  252. return err;
  253. dev->last_rekey = zone;
  254. }
  255. }
  256. grub_memset (iv, 0, sizeof (iv));
  257. switch (dev->mode_iv)
  258. {
  259. case GRUB_CRYPTODISK_MODE_IV_NULL:
  260. break;
  261. case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64_HASH:
  262. {
  263. grub_uint64_t tmp;
  264. void *ctx;
  265. ctx = grub_zalloc (dev->iv_hash->contextsize);
  266. if (!ctx)
  267. return GPG_ERR_OUT_OF_MEMORY;
  268. tmp = grub_cpu_to_le64 (sector << log_sector_size);
  269. dev->iv_hash->init (ctx);
  270. dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
  271. dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
  272. dev->iv_hash->final (ctx);
  273. grub_memcpy (iv, dev->iv_hash->read (ctx), sizeof (iv));
  274. grub_free (ctx);
  275. }
  276. break;
  277. case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
  278. case GRUB_CRYPTODISK_MODE_IV_PLAIN:
  279. /*
  280. * The IV is a 32 or 64 bit value of the dm-crypt native sector
  281. * number. If using 32 bit IV mode, zero out the most significant
  282. * 32 bits.
  283. */
  284. {
  285. grub_uint64_t iv64;
  286. iv64 = grub_cpu_to_le64 (sector << (log_sector_size
  287. - GRUB_CRYPTODISK_IV_LOG_SIZE));
  288. grub_set_unaligned64 (iv, iv64);
  289. if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
  290. iv[1] = 0;
  291. }
  292. break;
  293. case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
  294. /* The IV is the 64 bit byte offset of the sector. */
  295. iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1])
  296. - log_sector_size));
  297. iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
  298. & GRUB_TYPE_U_MAX (iv[0]));
  299. break;
  300. case GRUB_CRYPTODISK_MODE_IV_BENBI:
  301. {
  302. grub_uint64_t num = (sector << dev->benbi_log) + 1;
  303. iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0]));
  304. iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_U_MAX (iv[0]));
  305. }
  306. break;
  307. case GRUB_CRYPTODISK_MODE_IV_ESSIV:
  308. iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
  309. err = grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv,
  310. dev->cipher->cipher->blocksize);
  311. if (err)
  312. return err;
  313. }
  314. switch (dev->mode)
  315. {
  316. case GRUB_CRYPTODISK_MODE_CBC:
  317. if (do_encrypt)
  318. err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
  319. ((grub_size_t) 1 << log_sector_size), iv);
  320. else
  321. err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
  322. ((grub_size_t) 1 << log_sector_size), iv);
  323. if (err)
  324. return err;
  325. break;
  326. case GRUB_CRYPTODISK_MODE_PCBC:
  327. if (do_encrypt)
  328. err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
  329. ((grub_size_t) 1 << log_sector_size), iv);
  330. else
  331. err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
  332. ((grub_size_t) 1 << log_sector_size), iv);
  333. if (err)
  334. return err;
  335. break;
  336. case GRUB_CRYPTODISK_MODE_XTS:
  337. {
  338. unsigned j;
  339. err = grub_crypto_ecb_encrypt (dev->secondary_cipher, iv, iv,
  340. dev->cipher->cipher->blocksize);
  341. if (err)
  342. return err;
  343. for (j = 0; j < (1U << log_sector_size);
  344. j += dev->cipher->cipher->blocksize)
  345. {
  346. grub_crypto_xor (data + i + j, data + i + j, iv,
  347. dev->cipher->cipher->blocksize);
  348. if (do_encrypt)
  349. err = grub_crypto_ecb_encrypt (dev->cipher, data + i + j,
  350. data + i + j,
  351. dev->cipher->cipher->blocksize);
  352. else
  353. err = grub_crypto_ecb_decrypt (dev->cipher, data + i + j,
  354. data + i + j,
  355. dev->cipher->cipher->blocksize);
  356. if (err)
  357. return err;
  358. grub_crypto_xor (data + i + j, data + i + j, iv,
  359. dev->cipher->cipher->blocksize);
  360. gf_mul_x ((grub_uint8_t *) iv);
  361. }
  362. }
  363. break;
  364. case GRUB_CRYPTODISK_MODE_LRW:
  365. {
  366. struct lrw_sector sec;
  367. generate_lrw_sector (&sec, dev, (grub_uint8_t *) iv);
  368. lrw_xor (&sec, dev, data + i);
  369. if (do_encrypt)
  370. err = grub_crypto_ecb_encrypt (dev->cipher, data + i,
  371. data + i,
  372. (1U << log_sector_size));
  373. else
  374. err = grub_crypto_ecb_decrypt (dev->cipher, data + i,
  375. data + i,
  376. (1U << log_sector_size));
  377. if (err)
  378. return err;
  379. lrw_xor (&sec, dev, data + i);
  380. }
  381. break;
  382. case GRUB_CRYPTODISK_MODE_ECB:
  383. if (do_encrypt)
  384. err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
  385. (1U << log_sector_size));
  386. else
  387. err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
  388. (1U << log_sector_size));
  389. if (err)
  390. return err;
  391. break;
  392. default:
  393. return GPG_ERR_NOT_IMPLEMENTED;
  394. }
  395. sector++;
  396. }
  397. return GPG_ERR_NO_ERROR;
  398. }
  399. gcry_err_code_t
  400. grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
  401. grub_uint8_t * data, grub_size_t len,
  402. grub_disk_addr_t sector, grub_size_t log_sector_size)
  403. {
  404. return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
  405. }
  406. grub_err_t
  407. grub_cryptodisk_setcipher (grub_cryptodisk_t crypt, const char *ciphername, const char *ciphermode)
  408. {
  409. const char *cipheriv = NULL;
  410. grub_crypto_cipher_handle_t cipher = NULL, secondary_cipher = NULL;
  411. grub_crypto_cipher_handle_t essiv_cipher = NULL;
  412. const gcry_md_spec_t *essiv_hash = NULL;
  413. const struct gcry_cipher_spec *ciph;
  414. grub_cryptodisk_mode_t mode;
  415. grub_cryptodisk_mode_iv_t mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN64;
  416. int benbi_log = 0;
  417. grub_err_t ret = GRUB_ERR_NONE;
  418. ciph = grub_crypto_lookup_cipher_by_name (ciphername);
  419. if (!ciph)
  420. {
  421. ret = grub_error (GRUB_ERR_FILE_NOT_FOUND, "Cipher %s isn't available",
  422. ciphername);
  423. goto err;
  424. }
  425. /* Configure the cipher used for the bulk data. */
  426. cipher = grub_crypto_cipher_open (ciph);
  427. if (!cipher)
  428. {
  429. ret = grub_error (GRUB_ERR_FILE_NOT_FOUND, "Cipher %s could not be initialized",
  430. ciphername);
  431. goto err;
  432. }
  433. /* Configure the cipher mode. */
  434. if (grub_strcmp (ciphermode, "ecb") == 0)
  435. {
  436. mode = GRUB_CRYPTODISK_MODE_ECB;
  437. mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
  438. cipheriv = NULL;
  439. }
  440. else if (grub_strcmp (ciphermode, "plain") == 0)
  441. {
  442. mode = GRUB_CRYPTODISK_MODE_CBC;
  443. mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
  444. cipheriv = NULL;
  445. }
  446. else if (grub_memcmp (ciphermode, "cbc-", sizeof ("cbc-") - 1) == 0)
  447. {
  448. mode = GRUB_CRYPTODISK_MODE_CBC;
  449. cipheriv = ciphermode + sizeof ("cbc-") - 1;
  450. }
  451. else if (grub_memcmp (ciphermode, "pcbc-", sizeof ("pcbc-") - 1) == 0)
  452. {
  453. mode = GRUB_CRYPTODISK_MODE_PCBC;
  454. cipheriv = ciphermode + sizeof ("pcbc-") - 1;
  455. }
  456. else if (grub_memcmp (ciphermode, "xts-", sizeof ("xts-") - 1) == 0)
  457. {
  458. mode = GRUB_CRYPTODISK_MODE_XTS;
  459. cipheriv = ciphermode + sizeof ("xts-") - 1;
  460. secondary_cipher = grub_crypto_cipher_open (ciph);
  461. if (!secondary_cipher)
  462. {
  463. ret = grub_error (GRUB_ERR_FILE_NOT_FOUND,
  464. "Secondary cipher %s isn't available", ciphername);
  465. goto err;
  466. }
  467. if (cipher->cipher->blocksize != GRUB_CRYPTODISK_GF_BYTES)
  468. {
  469. ret = grub_error (GRUB_ERR_BAD_ARGUMENT,
  470. "Unsupported XTS block size: %" PRIuGRUB_SIZE,
  471. cipher->cipher->blocksize);
  472. goto err;
  473. }
  474. if (secondary_cipher->cipher->blocksize != GRUB_CRYPTODISK_GF_BYTES)
  475. {
  476. ret = grub_error (GRUB_ERR_BAD_ARGUMENT,
  477. "Unsupported XTS block size: %" PRIuGRUB_SIZE,
  478. secondary_cipher->cipher->blocksize);
  479. goto err;
  480. }
  481. }
  482. else if (grub_memcmp (ciphermode, "lrw-", sizeof ("lrw-") - 1) == 0)
  483. {
  484. mode = GRUB_CRYPTODISK_MODE_LRW;
  485. cipheriv = ciphermode + sizeof ("lrw-") - 1;
  486. if (cipher->cipher->blocksize != GRUB_CRYPTODISK_GF_BYTES)
  487. {
  488. ret = grub_error (GRUB_ERR_BAD_ARGUMENT,
  489. "Unsupported LRW block size: %" PRIuGRUB_SIZE,
  490. cipher->cipher->blocksize);
  491. goto err;
  492. }
  493. }
  494. else
  495. {
  496. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Unknown cipher mode: %s",
  497. ciphermode);
  498. goto err;
  499. }
  500. if (cipheriv == NULL)
  501. ;
  502. else if (grub_memcmp (cipheriv, "plain64", sizeof ("plain64") - 1) == 0)
  503. mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN64;
  504. else if (grub_memcmp (cipheriv, "plain", sizeof ("plain") - 1) == 0)
  505. mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
  506. else if (grub_memcmp (cipheriv, "benbi", sizeof ("benbi") - 1) == 0)
  507. {
  508. if (cipher->cipher->blocksize & (cipher->cipher->blocksize - 1)
  509. || cipher->cipher->blocksize == 0)
  510. grub_error (GRUB_ERR_BAD_ARGUMENT,
  511. "Unsupported benbi blocksize: %" PRIuGRUB_SIZE,
  512. cipher->cipher->blocksize);
  513. /* FIXME should we return an error here? */
  514. for (benbi_log = 0;
  515. (cipher->cipher->blocksize << benbi_log) < GRUB_DISK_SECTOR_SIZE;
  516. benbi_log++);
  517. mode_iv = GRUB_CRYPTODISK_MODE_IV_BENBI;
  518. }
  519. else if (grub_memcmp (cipheriv, "null", sizeof ("null") - 1) == 0)
  520. mode_iv = GRUB_CRYPTODISK_MODE_IV_NULL;
  521. else if (grub_memcmp (cipheriv, "essiv:", sizeof ("essiv:") - 1) == 0)
  522. {
  523. const char *hash_str = cipheriv + 6;
  524. mode_iv = GRUB_CRYPTODISK_MODE_IV_ESSIV;
  525. /* Configure the hash and cipher used for ESSIV. */
  526. essiv_hash = grub_crypto_lookup_md_by_name (hash_str);
  527. if (!essiv_hash)
  528. {
  529. ret = grub_error (GRUB_ERR_FILE_NOT_FOUND,
  530. "Couldn't load %s hash", hash_str);
  531. goto err;
  532. }
  533. essiv_cipher = grub_crypto_cipher_open (ciph);
  534. if (!essiv_cipher)
  535. {
  536. ret = grub_error (GRUB_ERR_FILE_NOT_FOUND,
  537. "Couldn't load %s cipher", ciphername);
  538. goto err;
  539. }
  540. }
  541. else
  542. {
  543. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Unknown IV mode: %s",
  544. cipheriv);
  545. goto err;
  546. }
  547. crypt->cipher = cipher;
  548. crypt->benbi_log = benbi_log;
  549. crypt->mode = mode;
  550. crypt->mode_iv = mode_iv;
  551. crypt->secondary_cipher = secondary_cipher;
  552. crypt->essiv_cipher = essiv_cipher;
  553. crypt->essiv_hash = essiv_hash;
  554. err:
  555. if (ret)
  556. {
  557. grub_crypto_cipher_close (cipher);
  558. grub_crypto_cipher_close (secondary_cipher);
  559. }
  560. return ret;
  561. }
  562. gcry_err_code_t
  563. grub_cryptodisk_setkey (grub_cryptodisk_t dev, grub_uint8_t *key, grub_size_t keysize)
  564. {
  565. gcry_err_code_t err;
  566. int real_keysize;
  567. real_keysize = keysize;
  568. if (dev->mode == GRUB_CRYPTODISK_MODE_XTS)
  569. real_keysize /= 2;
  570. if (dev->mode == GRUB_CRYPTODISK_MODE_LRW)
  571. real_keysize -= dev->cipher->cipher->blocksize;
  572. /* Set the PBKDF2 output as the cipher key. */
  573. err = grub_crypto_cipher_set_key (dev->cipher, key, real_keysize);
  574. if (err)
  575. return err;
  576. grub_memcpy (dev->key, key, keysize);
  577. dev->keysize = keysize;
  578. /* Configure ESSIV if necessary. */
  579. if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_ESSIV)
  580. {
  581. grub_size_t essiv_keysize = dev->essiv_hash->mdlen;
  582. grub_uint8_t hashed_key[GRUB_CRYPTO_MAX_MDLEN];
  583. if (essiv_keysize > GRUB_CRYPTO_MAX_MDLEN)
  584. return GPG_ERR_INV_ARG;
  585. grub_crypto_hash (dev->essiv_hash, hashed_key, key, keysize);
  586. err = grub_crypto_cipher_set_key (dev->essiv_cipher,
  587. hashed_key, essiv_keysize);
  588. if (err)
  589. return err;
  590. }
  591. if (dev->mode == GRUB_CRYPTODISK_MODE_XTS)
  592. {
  593. err = grub_crypto_cipher_set_key (dev->secondary_cipher,
  594. key + real_keysize,
  595. keysize / 2);
  596. if (err)
  597. return err;
  598. }
  599. if (dev->mode == GRUB_CRYPTODISK_MODE_LRW)
  600. {
  601. unsigned i;
  602. grub_uint8_t idx[GRUB_CRYPTODISK_GF_BYTES];
  603. grub_free (dev->lrw_precalc);
  604. grub_memcpy (dev->lrw_key, key + real_keysize,
  605. dev->cipher->cipher->blocksize);
  606. dev->lrw_precalc = grub_malloc ((1U << dev->log_sector_size));
  607. if (!dev->lrw_precalc)
  608. return GPG_ERR_OUT_OF_MEMORY;
  609. grub_memset (idx, 0, GRUB_CRYPTODISK_GF_BYTES);
  610. for (i = 0; i < (1U << dev->log_sector_size);
  611. i += GRUB_CRYPTODISK_GF_BYTES)
  612. {
  613. idx[GRUB_CRYPTODISK_GF_BYTES - 1] = i / GRUB_CRYPTODISK_GF_BYTES;
  614. gf_mul_be (dev->lrw_precalc + i, idx, dev->lrw_key);
  615. }
  616. }
  617. return GPG_ERR_NO_ERROR;
  618. }
  619. static int
  620. grub_cryptodisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  621. grub_disk_pull_t pull)
  622. {
  623. grub_cryptodisk_t i;
  624. if (pull != GRUB_DISK_PULL_NONE)
  625. return 0;
  626. for (i = cryptodisk_list; i != NULL; i = i->next)
  627. {
  628. char buf[30];
  629. grub_snprintf (buf, sizeof (buf), "crypto%lu", i->id);
  630. if (hook (buf, hook_data))
  631. return 1;
  632. }
  633. return GRUB_ERR_NONE;
  634. }
  635. static grub_err_t
  636. grub_cryptodisk_open (const char *name, grub_disk_t disk)
  637. {
  638. grub_cryptodisk_t dev;
  639. if (grub_memcmp (name, "crypto", sizeof ("crypto") - 1) != 0)
  640. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "No such device");
  641. if (grub_memcmp (name, "cryptouuid/", sizeof ("cryptouuid/") - 1) == 0)
  642. {
  643. for (dev = cryptodisk_list; dev != NULL; dev = dev->next)
  644. if (grub_uuidcasecmp (name + sizeof ("cryptouuid/") - 1, dev->uuid, sizeof (dev->uuid)) == 0)
  645. break;
  646. }
  647. else
  648. {
  649. unsigned long id = grub_strtoul (name + sizeof ("crypto") - 1, 0, 0);
  650. if (grub_errno)
  651. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "No such device");
  652. /* Search for requested device in the list of CRYPTODISK devices. */
  653. for (dev = cryptodisk_list; dev != NULL; dev = dev->next)
  654. if (dev->id == id)
  655. break;
  656. }
  657. if (!dev)
  658. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "No such device");
  659. #ifdef GRUB_UTIL
  660. if (dev->cheat)
  661. {
  662. grub_uint64_t cheat_dev_size;
  663. unsigned int cheat_log_sector_size;
  664. if (!GRUB_UTIL_FD_IS_VALID (dev->cheat_fd))
  665. dev->cheat_fd = grub_util_fd_open (dev->cheat, GRUB_UTIL_FD_O_RDONLY);
  666. if (!GRUB_UTIL_FD_IS_VALID (dev->cheat_fd))
  667. return grub_error (GRUB_ERR_IO, N_("cannot open `%s': %s"),
  668. dev->cheat, grub_util_fd_strerror ());
  669. /* Use the sector size and count of the cheat device. */
  670. cheat_dev_size = grub_util_get_fd_size (dev->cheat_fd, dev->cheat, &cheat_log_sector_size);
  671. if (cheat_dev_size == -1)
  672. {
  673. const char *errmsg = grub_util_fd_strerror ();
  674. grub_util_fd_close (dev->cheat_fd);
  675. dev->cheat_fd = GRUB_UTIL_FD_INVALID;
  676. return grub_error (GRUB_ERR_IO, N_("failed to query size of device `%s': %s"),
  677. dev->cheat, errmsg);
  678. }
  679. dev->log_sector_size = cheat_log_sector_size;
  680. dev->total_sectors = cheat_dev_size >> cheat_log_sector_size;
  681. }
  682. #endif
  683. if (!dev->source_disk)
  684. {
  685. grub_dprintf ("cryptodisk", "Opening device %s\n", name);
  686. /* Try to open the source disk and populate the requested disk. */
  687. dev->source_disk = grub_disk_open (dev->source);
  688. if (!dev->source_disk)
  689. return grub_errno;
  690. }
  691. disk->data = dev;
  692. disk->log_sector_size = dev->log_sector_size;
  693. disk->total_sectors = dev->total_sectors;
  694. disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
  695. disk->id = dev->id;
  696. dev->ref++;
  697. return GRUB_ERR_NONE;
  698. }
  699. static void
  700. grub_cryptodisk_close (grub_disk_t disk)
  701. {
  702. grub_cryptodisk_t dev = (grub_cryptodisk_t) disk->data;
  703. grub_dprintf ("cryptodisk", "Closing disk\n");
  704. dev->ref--;
  705. if (dev->ref != 0)
  706. return;
  707. #ifdef GRUB_UTIL
  708. if (dev->cheat)
  709. {
  710. grub_util_fd_close (dev->cheat_fd);
  711. dev->cheat_fd = GRUB_UTIL_FD_INVALID;
  712. }
  713. #endif
  714. grub_disk_close (dev->source_disk);
  715. dev->source_disk = NULL;
  716. }
  717. static grub_err_t
  718. grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
  719. grub_size_t size, char *buf)
  720. {
  721. grub_cryptodisk_t dev = (grub_cryptodisk_t) disk->data;
  722. grub_err_t err;
  723. gcry_err_code_t gcry_err;
  724. #ifdef GRUB_UTIL
  725. if (dev->cheat)
  726. {
  727. int r;
  728. r = grub_util_fd_seek (dev->cheat_fd, sector << disk->log_sector_size);
  729. if (r)
  730. return grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
  731. dev->cheat, grub_util_fd_strerror ());
  732. if (grub_util_fd_read (dev->cheat_fd, buf, size << disk->log_sector_size)
  733. != (ssize_t) (size << disk->log_sector_size))
  734. return grub_error (GRUB_ERR_READ_ERROR, N_("cannot read `%s': %s"),
  735. dev->cheat, grub_util_fd_strerror ());
  736. return GRUB_ERR_NONE;
  737. }
  738. #endif
  739. grub_dprintf ("cryptodisk",
  740. "Reading %" PRIuGRUB_SIZE " sectors from sector 0x%"
  741. PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
  742. size, sector, dev->offset_sectors);
  743. err = grub_disk_read (dev->source_disk,
  744. grub_disk_from_native_sector (disk, sector + dev->offset_sectors),
  745. 0, size << disk->log_sector_size, buf);
  746. if (err)
  747. {
  748. grub_dprintf ("cryptodisk", "grub_disk_read failed with error %d\n", err);
  749. return err;
  750. }
  751. gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
  752. size << disk->log_sector_size,
  753. sector, dev->log_sector_size, 0);
  754. return grub_crypto_gcry_error (gcry_err);
  755. }
  756. static grub_err_t
  757. grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
  758. grub_size_t size, const char *buf)
  759. {
  760. grub_cryptodisk_t dev = (grub_cryptodisk_t) disk->data;
  761. gcry_err_code_t gcry_err;
  762. char *tmp;
  763. grub_err_t err;
  764. #ifdef GRUB_UTIL
  765. if (dev->cheat)
  766. {
  767. int r;
  768. r = grub_util_fd_seek (dev->cheat_fd, sector << disk->log_sector_size);
  769. if (r)
  770. return grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
  771. dev->cheat, grub_util_fd_strerror ());
  772. if (grub_util_fd_write (dev->cheat_fd, buf, size << disk->log_sector_size)
  773. != (ssize_t) (size << disk->log_sector_size))
  774. return grub_error (GRUB_ERR_READ_ERROR, N_("cannot read `%s': %s"),
  775. dev->cheat, grub_util_fd_strerror ());
  776. return GRUB_ERR_NONE;
  777. }
  778. #endif
  779. tmp = grub_malloc (size << disk->log_sector_size);
  780. if (!tmp)
  781. return grub_errno;
  782. grub_memcpy (tmp, buf, size << disk->log_sector_size);
  783. grub_dprintf ("cryptodisk",
  784. "Writing %" PRIuGRUB_SIZE " sectors to sector 0x%"
  785. PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
  786. size, sector, dev->offset_sectors);
  787. gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
  788. size << disk->log_sector_size,
  789. sector, disk->log_sector_size, 1);
  790. if (gcry_err)
  791. {
  792. grub_free (tmp);
  793. return grub_crypto_gcry_error (gcry_err);
  794. }
  795. /* Since ->write was called so disk.mod is loaded but be paranoid */
  796. sector = sector + dev->offset_sectors;
  797. if (grub_disk_write_weak)
  798. err = grub_disk_write_weak (dev->source_disk,
  799. grub_disk_from_native_sector (disk, sector),
  800. 0, size << disk->log_sector_size, tmp);
  801. else
  802. err = grub_error (GRUB_ERR_BUG, "disk.mod not loaded");
  803. grub_free (tmp);
  804. return err;
  805. }
  806. #ifdef GRUB_UTIL
  807. static grub_disk_memberlist_t
  808. grub_cryptodisk_memberlist (grub_disk_t disk)
  809. {
  810. grub_cryptodisk_t dev = (grub_cryptodisk_t) disk->data;
  811. grub_disk_memberlist_t list = NULL;
  812. list = grub_malloc (sizeof (*list));
  813. if (list)
  814. {
  815. list->disk = dev->source_disk;
  816. list->next = NULL;
  817. }
  818. return list;
  819. }
  820. #endif
  821. static void
  822. cryptodisk_cleanup (void)
  823. {
  824. #if 0
  825. grub_cryptodisk_t dev = cryptodisk_list;
  826. grub_cryptodisk_t tmp;
  827. while (dev != NULL)
  828. {
  829. grub_free (dev->source);
  830. grub_free (dev->cipher);
  831. grub_free (dev->secondary_cipher);
  832. grub_free (dev->essiv_cipher);
  833. tmp = dev->next;
  834. grub_free (dev);
  835. dev = tmp;
  836. }
  837. #endif
  838. }
  839. grub_err_t
  840. grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
  841. grub_disk_t source)
  842. {
  843. newdev->source = grub_strdup (name);
  844. if (!newdev->source)
  845. return grub_errno;
  846. newdev->id = last_cryptodisk_id++;
  847. newdev->source_id = source->id;
  848. newdev->source_dev_id = source->dev->id;
  849. newdev->partition_start = grub_partition_get_start (source->partition);
  850. newdev->next = cryptodisk_list;
  851. cryptodisk_list = newdev;
  852. return GRUB_ERR_NONE;
  853. }
  854. grub_cryptodisk_t
  855. grub_cryptodisk_get_by_uuid (const char *uuid)
  856. {
  857. grub_cryptodisk_t dev;
  858. for (dev = cryptodisk_list; dev != NULL; dev = dev->next)
  859. if (grub_uuidcasecmp (dev->uuid, uuid, sizeof (dev->uuid)) == 0)
  860. return dev;
  861. return NULL;
  862. }
  863. grub_cryptodisk_t
  864. grub_cryptodisk_get_by_source_disk (grub_disk_t disk)
  865. {
  866. grub_cryptodisk_t dev;
  867. for (dev = cryptodisk_list; dev != NULL; dev = dev->next)
  868. if (dev->source_id == disk->id && dev->source_dev_id == disk->dev->id)
  869. if ((disk->partition && grub_partition_get_start (disk->partition) == dev->partition_start) ||
  870. (!disk->partition && dev->partition_start == 0))
  871. return dev;
  872. return NULL;
  873. }
  874. #ifdef GRUB_UTIL
  875. grub_err_t
  876. grub_cryptodisk_cheat_insert (grub_cryptodisk_t newdev, const char *name,
  877. grub_disk_t source, const char *cheat)
  878. {
  879. newdev->cheat = grub_strdup (cheat);
  880. newdev->source = grub_strdup (name);
  881. if (!newdev->source || !newdev->cheat)
  882. {
  883. grub_free (newdev->source);
  884. grub_free (newdev->cheat);
  885. return grub_errno;
  886. }
  887. newdev->cheat_fd = GRUB_UTIL_FD_INVALID;
  888. newdev->source_id = source->id;
  889. newdev->source_dev_id = source->dev->id;
  890. newdev->partition_start = grub_partition_get_start (source->partition);
  891. newdev->id = last_cryptodisk_id++;
  892. newdev->next = cryptodisk_list;
  893. cryptodisk_list = newdev;
  894. return GRUB_ERR_NONE;
  895. }
  896. void
  897. grub_util_cryptodisk_get_abstraction (grub_disk_t disk,
  898. void (*cb) (const char *val, void *data),
  899. void *data)
  900. {
  901. grub_cryptodisk_t dev = (grub_cryptodisk_t) disk->data;
  902. cb ("cryptodisk", data);
  903. cb (dev->modname, data);
  904. if (dev->cipher)
  905. cb (dev->cipher->cipher->modname, data);
  906. if (dev->secondary_cipher)
  907. cb (dev->secondary_cipher->cipher->modname, data);
  908. if (dev->essiv_cipher)
  909. cb (dev->essiv_cipher->cipher->modname, data);
  910. if (dev->hash)
  911. cb (dev->hash->modname, data);
  912. if (dev->essiv_hash)
  913. cb (dev->essiv_hash->modname, data);
  914. if (dev->iv_hash)
  915. cb (dev->iv_hash->modname, data);
  916. }
  917. const char *
  918. grub_util_cryptodisk_get_uuid (grub_disk_t disk)
  919. {
  920. grub_cryptodisk_t dev = (grub_cryptodisk_t) disk->data;
  921. return dev->uuid;
  922. }
  923. #endif
  924. static void
  925. cryptodisk_close (grub_cryptodisk_t dev)
  926. {
  927. grub_crypto_cipher_close (dev->cipher);
  928. grub_crypto_cipher_close (dev->secondary_cipher);
  929. grub_crypto_cipher_close (dev->essiv_cipher);
  930. grub_free (dev);
  931. }
  932. static grub_err_t
  933. cryptodisk_read_hook (grub_disk_addr_t sector, unsigned offset,
  934. unsigned length, char *buf, void *data)
  935. {
  936. cryptodisk_read_hook_ctx_t ctx = data;
  937. if (ctx->hdr_file == NULL)
  938. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("header file not found"));
  939. if (grub_file_seek (ctx->hdr_file,
  940. ((sector - ctx->part_start) * GRUB_DISK_SECTOR_SIZE) + offset)
  941. == (grub_off_t) -1)
  942. return grub_errno;
  943. if (grub_file_read (ctx->hdr_file, buf, length) != (grub_ssize_t) length)
  944. {
  945. if (grub_errno == GRUB_ERR_NONE)
  946. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("header file too small"));
  947. return grub_errno;
  948. }
  949. return GRUB_ERR_NONE;
  950. }
  951. static grub_cryptodisk_t
  952. grub_cryptodisk_scan_device_real (const char *name,
  953. grub_disk_t source,
  954. grub_cryptomount_args_t cargs)
  955. {
  956. grub_err_t ret = GRUB_ERR_NONE;
  957. grub_cryptodisk_t dev;
  958. grub_cryptodisk_dev_t cr;
  959. struct cryptodisk_read_hook_ctx read_hook_data = {0};
  960. int askpass = 0;
  961. char *part = NULL;
  962. dev = grub_cryptodisk_get_by_source_disk (source);
  963. if (dev)
  964. return dev;
  965. if (cargs->hdr_file != NULL)
  966. {
  967. /*
  968. * Set read hook to read header from a file instead of the source disk.
  969. * Disk read hooks are executed after the data has been read from the
  970. * disk. This is okay, because the read hook is given the read buffer
  971. * before its sent back to the caller. In this case, the hook can then
  972. * overwrite the data read from the disk device with data from the
  973. * header file sent in as the read hook data. This is transparent to the
  974. * read caller. Since the callers of this function have just opened the
  975. * source disk, there are no current read hooks, so there's no need to
  976. * save/restore them nor consider if they should be called or not.
  977. *
  978. * This hook assumes that the header is at the start of the volume, which
  979. * is not the case for some formats (eg. GELI). It also can only be used
  980. * with formats where the detached header file can be written to the
  981. * first blocks of the volume and the volume could still be unlocked.
  982. * So the header file can not be formatted differently from the on-disk
  983. * header. If these assumpts are not met, detached header file processing
  984. * must be specially handled in the cryptodisk backend module.
  985. *
  986. * This hook needs only be set once and will be called potentially many
  987. * times by a backend. This is fine because of the assumptions mentioned
  988. * and the read hook reads from absolute offsets and is stateless.
  989. */
  990. read_hook_data.part_start = grub_partition_get_start (source->partition);
  991. read_hook_data.hdr_file = cargs->hdr_file;
  992. source->read_hook = cryptodisk_read_hook;
  993. source->read_hook_data = (void *) &read_hook_data;
  994. }
  995. FOR_CRYPTODISK_DEVS (cr)
  996. {
  997. /*
  998. * Loop through each cryptodisk backend that is registered (ie. loaded).
  999. * If the scan returns NULL, then the backend being tested does not
  1000. * recognize the source disk, so move on to the next backend.
  1001. */
  1002. dev = cr->scan (source, cargs);
  1003. if (grub_errno)
  1004. goto error_no_close;
  1005. if (!dev)
  1006. continue;
  1007. if (cargs->key_len)
  1008. {
  1009. ret = cr->recover_key (source, dev, cargs);
  1010. if (ret != GRUB_ERR_NONE)
  1011. goto error;
  1012. }
  1013. else
  1014. {
  1015. /* Get the passphrase from the user, if no key data. */
  1016. unsigned long tries = 3;
  1017. const char *tries_env;
  1018. askpass = 1;
  1019. cargs->key_data = grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE);
  1020. if (cargs->key_data == NULL)
  1021. goto error_no_close;
  1022. tries_env = grub_env_get ("cryptodisk_passphrase_tries");
  1023. if (tries_env != NULL && tries_env[0] != '\0')
  1024. {
  1025. unsigned long tries_env_val;
  1026. const char *p;
  1027. tries_env_val = grub_strtoul (tries_env, &p, 0);
  1028. if (*p == '\0' && tries_env_val != ~0UL)
  1029. tries = tries_env_val;
  1030. else
  1031. grub_printf_ (N_("Invalid cryptodisk_passphrase_tries value `%s'. Defaulting to %lu.\n"),
  1032. tries_env,
  1033. tries);
  1034. }
  1035. for (; tries > 0; tries--)
  1036. {
  1037. part = grub_partition_get_name (source->partition);
  1038. grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
  1039. source->partition != NULL ? "," : "",
  1040. part != NULL ? part : N_("UNKNOWN"),
  1041. dev->uuid);
  1042. grub_free (part);
  1043. if (!grub_password_get ((char *) cargs->key_data, GRUB_CRYPTODISK_MAX_PASSPHRASE))
  1044. {
  1045. grub_error (GRUB_ERR_BAD_ARGUMENT, "passphrase not supplied");
  1046. goto error;
  1047. }
  1048. cargs->key_len = grub_strlen ((char *) cargs->key_data);
  1049. ret = cr->recover_key (source, dev, cargs);
  1050. if (ret == GRUB_ERR_NONE)
  1051. break;
  1052. if (ret != GRUB_ERR_ACCESS_DENIED || tries == 1)
  1053. goto error;
  1054. grub_puts_ (N_("Invalid passphrase."));
  1055. /*
  1056. * Since recover_key() calls a function that returns grub_errno,
  1057. * a leftover error value from a previously rejected passphrase
  1058. * will trigger a phantom failure. We therefore clear it before
  1059. * trying a new passphrase.
  1060. */
  1061. grub_errno = GRUB_ERR_NONE;
  1062. }
  1063. }
  1064. ret = grub_cryptodisk_insert (dev, name, source);
  1065. if (ret != GRUB_ERR_NONE)
  1066. goto error;
  1067. goto cleanup;
  1068. }
  1069. grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk module can handle this device");
  1070. goto cleanup;
  1071. error:
  1072. cryptodisk_close (dev);
  1073. error_no_close:
  1074. dev = NULL;
  1075. cleanup:
  1076. if (cargs->hdr_file != NULL)
  1077. source->read_hook = NULL;
  1078. if (askpass)
  1079. {
  1080. cargs->key_len = 0;
  1081. grub_free (cargs->key_data);
  1082. }
  1083. return dev;
  1084. }
  1085. #ifdef GRUB_UTIL
  1086. #include <grub/util/misc.h>
  1087. grub_err_t
  1088. grub_cryptodisk_cheat_mount (const char *sourcedev, const char *cheat)
  1089. {
  1090. grub_err_t err;
  1091. grub_cryptodisk_t dev;
  1092. grub_cryptodisk_dev_t cr;
  1093. grub_disk_t source;
  1094. struct grub_cryptomount_args cargs = {0};
  1095. /* Try to open disk. */
  1096. source = grub_disk_open (sourcedev);
  1097. if (!source)
  1098. return grub_errno;
  1099. dev = grub_cryptodisk_get_by_source_disk (source);
  1100. if (dev)
  1101. {
  1102. grub_disk_close (source);
  1103. return GRUB_ERR_NONE;
  1104. }
  1105. FOR_CRYPTODISK_DEVS (cr)
  1106. {
  1107. dev = cr->scan (source, &cargs);
  1108. if (grub_errno)
  1109. return grub_errno;
  1110. if (!dev)
  1111. continue;
  1112. grub_util_info ("cheatmounted %s (%s) at %s", sourcedev, dev->modname,
  1113. cheat);
  1114. err = grub_cryptodisk_cheat_insert (dev, sourcedev, source, cheat);
  1115. grub_disk_close (source);
  1116. if (err)
  1117. grub_free (dev);
  1118. return GRUB_ERR_NONE;
  1119. }
  1120. grub_disk_close (source);
  1121. return GRUB_ERR_NONE;
  1122. }
  1123. #endif
  1124. static int
  1125. grub_cryptodisk_scan_device (const char *name,
  1126. void *data)
  1127. {
  1128. int ret = 0;
  1129. grub_disk_t source;
  1130. grub_cryptodisk_t dev;
  1131. grub_cryptomount_args_t cargs = data;
  1132. grub_errno = GRUB_ERR_NONE;
  1133. /* Try to open disk. */
  1134. source = grub_disk_open (name);
  1135. if (!source)
  1136. {
  1137. grub_print_error ();
  1138. return 0;
  1139. }
  1140. dev = grub_cryptodisk_scan_device_real (name, source, cargs);
  1141. if (dev)
  1142. {
  1143. ret = (cargs->search_uuid != NULL
  1144. && grub_uuidcasecmp (cargs->search_uuid, dev->uuid, sizeof (dev->uuid)) == 0);
  1145. goto cleanup;
  1146. }
  1147. /*
  1148. * Do not print error when err is GRUB_ERR_BAD_MODULE to avoid many unhelpful
  1149. * error messages.
  1150. */
  1151. if (grub_errno == GRUB_ERR_BAD_MODULE)
  1152. grub_error_pop ();
  1153. if (cargs->search_uuid != NULL)
  1154. /* Push error onto stack to save for cryptomount. */
  1155. grub_error_push ();
  1156. else
  1157. grub_print_error ();
  1158. cleanup:
  1159. grub_disk_close (source);
  1160. return ret;
  1161. }
  1162. static grub_err_t
  1163. grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
  1164. {
  1165. struct grub_arg_list *state = ctxt->state;
  1166. struct grub_cryptomount_args cargs = {0};
  1167. if (argc < 1 && !state[OPTION_ALL].set && !state[OPTION_BOOT].set)
  1168. return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
  1169. if (grub_cryptodisk_list == NULL)
  1170. return grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk modules loaded");
  1171. if (state[OPTION_PASSWORD].set) /* password */
  1172. {
  1173. cargs.key_data = (grub_uint8_t *) state[OPTION_PASSWORD].arg;
  1174. cargs.key_len = grub_strlen (state[OPTION_PASSWORD].arg);
  1175. }
  1176. if (state[OPTION_KEYFILE].set) /* keyfile */
  1177. {
  1178. const char *p = NULL;
  1179. grub_file_t keyfile;
  1180. unsigned long long keyfile_offset = 0, keyfile_size = 0;
  1181. if (state[OPTION_KEYFILE_OFFSET].set) /* keyfile-offset */
  1182. {
  1183. grub_errno = GRUB_ERR_NONE;
  1184. keyfile_offset = grub_strtoull (state[OPTION_KEYFILE_OFFSET].arg, &p, 0);
  1185. if (state[OPTION_KEYFILE_OFFSET].arg[0] == '\0' || *p != '\0')
  1186. return grub_error (grub_errno,
  1187. N_("non-numeric or invalid keyfile offset `%s'"),
  1188. state[OPTION_KEYFILE_OFFSET].arg);
  1189. }
  1190. if (state[OPTION_KEYFILE_SIZE].set) /* keyfile-size */
  1191. {
  1192. grub_errno = GRUB_ERR_NONE;
  1193. keyfile_size = grub_strtoull (state[OPTION_KEYFILE_SIZE].arg, &p, 0);
  1194. if (state[OPTION_KEYFILE_SIZE].arg[0] == '\0' || *p != '\0')
  1195. return grub_error (grub_errno,
  1196. N_("non-numeric or invalid keyfile size `%s'"),
  1197. state[OPTION_KEYFILE_SIZE].arg);
  1198. if (keyfile_size == 0)
  1199. return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("key file size is 0"));
  1200. if (keyfile_size > GRUB_CRYPTODISK_MAX_KEYFILE_SIZE)
  1201. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  1202. N_("key file size exceeds maximum (%d)"),
  1203. GRUB_CRYPTODISK_MAX_KEYFILE_SIZE);
  1204. }
  1205. keyfile = grub_file_open (state[OPTION_KEYFILE].arg,
  1206. GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY);
  1207. if (keyfile == NULL)
  1208. return grub_errno;
  1209. if (keyfile_offset > keyfile->size)
  1210. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  1211. N_("Keyfile offset, %llu, is greater than "
  1212. "keyfile size, %llu"),
  1213. keyfile_offset, (unsigned long long) keyfile->size);
  1214. if (grub_file_seek (keyfile, (grub_off_t) keyfile_offset) == (grub_off_t) -1)
  1215. return grub_errno;
  1216. if (keyfile_size != 0)
  1217. {
  1218. if (keyfile_size > (keyfile->size - keyfile_offset))
  1219. return grub_error (GRUB_ERR_FILE_READ_ERROR,
  1220. N_("keyfile is too small: requested %llu bytes,"
  1221. " but the file only has %" PRIuGRUB_UINT64_T
  1222. " bytes left at offset %llu"),
  1223. keyfile_size,
  1224. (grub_off_t) (keyfile->size - keyfile_offset),
  1225. keyfile_offset);
  1226. cargs.key_len = keyfile_size;
  1227. }
  1228. else
  1229. cargs.key_len = keyfile->size - keyfile_offset;
  1230. cargs.key_data = grub_malloc (cargs.key_len);
  1231. if (cargs.key_data == NULL)
  1232. return GRUB_ERR_OUT_OF_MEMORY;
  1233. if (grub_file_read (keyfile, cargs.key_data, cargs.key_len) != (grub_ssize_t) cargs.key_len)
  1234. return grub_error (GRUB_ERR_FILE_READ_ERROR, (N_("failed to read key file")));
  1235. }
  1236. if (state[OPTION_HEADER].set) /* header */
  1237. {
  1238. if (state[OPTION_UUID].set)
  1239. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  1240. N_("cannot use UUID lookup with detached header"));
  1241. cargs.hdr_file = grub_file_open (state[OPTION_HEADER].arg,
  1242. GRUB_FILE_TYPE_CRYPTODISK_DETACHED_HEADER);
  1243. if (cargs.hdr_file == NULL)
  1244. return grub_errno;
  1245. }
  1246. if (state[OPTION_UUID].set) /* uuid */
  1247. {
  1248. int found_uuid;
  1249. grub_cryptodisk_t dev;
  1250. dev = grub_cryptodisk_get_by_uuid (args[0]);
  1251. if (dev)
  1252. {
  1253. grub_dprintf ("cryptodisk",
  1254. "already mounted as crypto%lu\n", dev->id);
  1255. return GRUB_ERR_NONE;
  1256. }
  1257. cargs.check_boot = state[OPTION_BOOT].set;
  1258. cargs.search_uuid = args[0];
  1259. found_uuid = grub_device_iterate (&grub_cryptodisk_scan_device, &cargs);
  1260. if (found_uuid)
  1261. return GRUB_ERR_NONE;
  1262. else if (grub_errno == GRUB_ERR_NONE)
  1263. {
  1264. /*
  1265. * Try to pop the next error on the stack. If there is not one, then
  1266. * no device matched the given UUID.
  1267. */
  1268. grub_error_pop ();
  1269. if (grub_errno == GRUB_ERR_NONE)
  1270. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such cryptodisk found, perhaps a needed disk or cryptodisk module is not loaded");
  1271. }
  1272. return grub_errno;
  1273. }
  1274. else if (state[OPTION_ALL].set || (argc == 0 && state[OPTION_BOOT].set)) /* -a|-b */
  1275. {
  1276. cargs.check_boot = state[OPTION_BOOT].set;
  1277. grub_device_iterate (&grub_cryptodisk_scan_device, &cargs);
  1278. return GRUB_ERR_NONE;
  1279. }
  1280. else
  1281. {
  1282. grub_disk_t disk;
  1283. grub_cryptodisk_t dev;
  1284. char *diskname;
  1285. char *disklast = NULL;
  1286. grub_size_t len;
  1287. cargs.check_boot = state[OPTION_BOOT].set;
  1288. diskname = args[0];
  1289. len = grub_strlen (diskname);
  1290. if (len && diskname[0] == '(' && diskname[len - 1] == ')')
  1291. {
  1292. disklast = &diskname[len - 1];
  1293. *disklast = '\0';
  1294. diskname++;
  1295. }
  1296. disk = grub_disk_open (diskname);
  1297. if (!disk)
  1298. {
  1299. if (disklast)
  1300. *disklast = ')';
  1301. return grub_errno;
  1302. }
  1303. dev = grub_cryptodisk_get_by_source_disk (disk);
  1304. if (dev)
  1305. {
  1306. grub_dprintf ("cryptodisk", "already mounted as crypto%lu\n", dev->id);
  1307. grub_disk_close (disk);
  1308. if (disklast)
  1309. *disklast = ')';
  1310. return GRUB_ERR_NONE;
  1311. }
  1312. dev = grub_cryptodisk_scan_device_real (diskname, disk, &cargs);
  1313. grub_disk_close (disk);
  1314. if (disklast)
  1315. *disklast = ')';
  1316. return (dev == NULL) ? grub_errno : GRUB_ERR_NONE;
  1317. }
  1318. }
  1319. static struct grub_disk_dev grub_cryptodisk_dev = {
  1320. .name = "cryptodisk",
  1321. .id = GRUB_DISK_DEVICE_CRYPTODISK_ID,
  1322. .disk_iterate = grub_cryptodisk_iterate,
  1323. .disk_open = grub_cryptodisk_open,
  1324. .disk_close = grub_cryptodisk_close,
  1325. .disk_read = grub_cryptodisk_read,
  1326. .disk_write = grub_cryptodisk_write,
  1327. #ifdef GRUB_UTIL
  1328. .disk_memberlist = grub_cryptodisk_memberlist,
  1329. #endif
  1330. .next = 0
  1331. };
  1332. static char
  1333. hex (grub_uint8_t val)
  1334. {
  1335. if (val < 10)
  1336. return '0' + val;
  1337. return 'a' + val - 10;
  1338. }
  1339. /* Open a file named NAME and initialize FILE. */
  1340. static char *
  1341. luks_script_get (grub_size_t *sz)
  1342. {
  1343. grub_cryptodisk_t i;
  1344. grub_size_t size = 0;
  1345. char *ptr, *ret;
  1346. *sz = 0;
  1347. for (i = cryptodisk_list; i != NULL; i = i->next)
  1348. if (grub_strcmp (i->modname, "luks") == 0 ||
  1349. grub_strcmp (i->modname, "luks2") == 0)
  1350. {
  1351. size += grub_strlen (i->modname);
  1352. size += sizeof ("_mount");
  1353. size += grub_strlen (i->uuid);
  1354. size += grub_strlen (i->cipher->cipher->name);
  1355. /*
  1356. * Add space in the line for (in order) spaces, cipher mode, cipher IV
  1357. * mode, sector offset, sector size and the trailing newline. This is
  1358. * an upper bound on the size of this data. There are 15 extra bytes
  1359. * in an earlier version of this code that are unaccounted for. It is
  1360. * left in the calculations in case it is needed. At worst, its short-
  1361. * lived wasted space.
  1362. */
  1363. size += 5 + 5 + 8 + 20 + 6 + 1 + 15;
  1364. if (i->essiv_hash)
  1365. size += grub_strlen (i->essiv_hash->name);
  1366. size += i->keysize * 2;
  1367. }
  1368. ret = grub_malloc (size + 1);
  1369. if (!ret)
  1370. return 0;
  1371. ptr = ret;
  1372. for (i = cryptodisk_list; i != NULL; i = i->next)
  1373. if (grub_strcmp (i->modname, "luks") == 0 ||
  1374. grub_strcmp (i->modname, "luks2") == 0)
  1375. {
  1376. unsigned j;
  1377. const char *iptr;
  1378. ptr = grub_stpcpy (ptr, i->modname);
  1379. ptr = grub_stpcpy (ptr, "_mount ");
  1380. ptr = grub_stpcpy (ptr, i->uuid);
  1381. *ptr++ = ' ';
  1382. ptr += grub_snprintf (ptr, 21, "%" PRIxGRUB_OFFSET, i->offset_sectors);
  1383. *ptr++ = ' ';
  1384. ptr += grub_snprintf (ptr, 7, "%u", 1 << i->log_sector_size);
  1385. *ptr++ = ' ';
  1386. for (iptr = i->cipher->cipher->name; *iptr; iptr++)
  1387. *ptr++ = grub_tolower (*iptr);
  1388. switch (i->mode)
  1389. {
  1390. case GRUB_CRYPTODISK_MODE_ECB:
  1391. ptr = grub_stpcpy (ptr, "-ecb");
  1392. break;
  1393. case GRUB_CRYPTODISK_MODE_CBC:
  1394. ptr = grub_stpcpy (ptr, "-cbc");
  1395. break;
  1396. case GRUB_CRYPTODISK_MODE_PCBC:
  1397. ptr = grub_stpcpy (ptr, "-pcbc");
  1398. break;
  1399. case GRUB_CRYPTODISK_MODE_XTS:
  1400. ptr = grub_stpcpy (ptr, "-xts");
  1401. break;
  1402. case GRUB_CRYPTODISK_MODE_LRW:
  1403. ptr = grub_stpcpy (ptr, "-lrw");
  1404. break;
  1405. }
  1406. switch (i->mode_iv)
  1407. {
  1408. case GRUB_CRYPTODISK_MODE_IV_NULL:
  1409. ptr = grub_stpcpy (ptr, "-null");
  1410. break;
  1411. case GRUB_CRYPTODISK_MODE_IV_PLAIN:
  1412. ptr = grub_stpcpy (ptr, "-plain");
  1413. break;
  1414. case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
  1415. ptr = grub_stpcpy (ptr, "-plain64");
  1416. break;
  1417. case GRUB_CRYPTODISK_MODE_IV_BENBI:
  1418. ptr = grub_stpcpy (ptr, "-benbi");
  1419. break;
  1420. case GRUB_CRYPTODISK_MODE_IV_ESSIV:
  1421. ptr = grub_stpcpy (ptr, "-essiv:");
  1422. ptr = grub_stpcpy (ptr, i->essiv_hash->name);
  1423. break;
  1424. case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
  1425. case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64_HASH:
  1426. break;
  1427. }
  1428. *ptr++ = ' ';
  1429. for (j = 0; j < i->keysize; j++)
  1430. {
  1431. *ptr++ = hex (i->key[j] >> 4);
  1432. *ptr++ = hex (i->key[j] & 0xf);
  1433. }
  1434. *ptr++ = '\n';
  1435. }
  1436. *ptr = '\0';
  1437. *sz = ptr - ret;
  1438. return ret;
  1439. }
  1440. struct grub_procfs_entry luks_script =
  1441. {
  1442. .name = "luks_script",
  1443. .get_contents = luks_script_get
  1444. };
  1445. static grub_extcmd_t cmd;
  1446. GRUB_MOD_INIT (cryptodisk)
  1447. {
  1448. grub_disk_dev_register (&grub_cryptodisk_dev);
  1449. cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0,
  1450. N_("[ [-p password] | [-k keyfile"
  1451. " [-O keyoffset] [-S keysize] ] ] [-H file]"
  1452. " <SOURCE|-u UUID|-a|-b>"),
  1453. N_("Mount a crypto device."), options);
  1454. grub_procfs_register ("luks_script", &luks_script);
  1455. }
  1456. GRUB_MOD_FINI (cryptodisk)
  1457. {
  1458. grub_disk_dev_unregister (&grub_cryptodisk_dev);
  1459. cryptodisk_cleanup ();
  1460. grub_unregister_extcmd (cmd);
  1461. grub_procfs_unregister (&luks_script);
  1462. }