v6-0004-cryptodisk-add-support-for-LUKS1-detached-headers.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. From 53ad601c16ca9e718c459141b70e417634d0a76b Mon Sep 17 00:00:00 2001
  2. From: John Lane <john@lane.uk.net>
  3. Date: Tue, 23 Jun 2015 11:16:30 +0100
  4. Subject: [PATCH v6 4/6] cryptodisk: add support for LUKS1 detached headers
  5. cryptsetup supports having a detached header through the
  6. --header command line argument for both LUKS1 and LUKS2.
  7. This adds support for LUKS1 detached headers.
  8. Signed-off-by: John Lane <john@lane.uk.net>
  9. GNUtoo@cyberdimension.org: rebase, small fixes, commit message
  10. Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
  11. Reviewed-by: Patrick Steinhardt <ps@pks.im>
  12. ---
  13. grub-core/disk/luks.c | 48 ++++++++++++++++++++++++++++++-------------
  14. 1 file changed, 34 insertions(+), 14 deletions(-)
  15. diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c
  16. index 685235565..6286302e7 100644
  17. --- a/grub-core/disk/luks.c
  18. +++ b/grub-core/disk/luks.c
  19. @@ -23,6 +23,7 @@
  20. #include <grub/dl.h>
  21. #include <grub/err.h>
  22. #include <grub/disk.h>
  23. +#include <grub/file.h>
  24. #include <grub/crypto.h>
  25. #include <grub/partition.h>
  26. #include <grub/i18n.h>
  27. @@ -76,17 +77,23 @@ luks_scan (grub_disk_t disk, const char *check_uuid, int check_boot,
  28. char ciphername[sizeof (header.cipherName) + 1];
  29. char ciphermode[sizeof (header.cipherMode) + 1];
  30. char hashspec[sizeof (header.hashSpec) + 1];
  31. - grub_err_t err;
  32. -
  33. - /* Detached headers are not implemented yet */
  34. - if (hdr)
  35. - return NULL;
  36. + grub_err_t err = GRUB_ERR_NONE;
  37. if (check_boot)
  38. return NULL;
  39. /* Read the LUKS header. */
  40. - err = grub_disk_read (disk, 0, 0, sizeof (header), &header);
  41. + if (hdr)
  42. + {
  43. + if (grub_file_seek (hdr, 0) == (grub_off_t) -1)
  44. + return NULL;
  45. +
  46. + if (grub_file_read (hdr, &header, sizeof (header)) != sizeof (header))
  47. + return NULL;
  48. + }
  49. + else
  50. + err = grub_disk_read (disk, 0, 0, sizeof (header), &header);
  51. +
  52. if (err)
  53. {
  54. if (err == GRUB_ERR_OUT_OF_RANGE)
  55. @@ -163,15 +170,22 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr)
  56. grub_uint8_t candidate_digest[sizeof (header.mkDigest)];
  57. unsigned i;
  58. grub_size_t length;
  59. - grub_err_t err;
  60. + grub_err_t err = GRUB_ERR_NONE;
  61. grub_size_t max_stripes = 1;
  62. char *tmp;
  63. + grub_uint32_t sector;
  64. - /* Detached headers are not implemented yet */
  65. if (hdr)
  66. - return GRUB_ERR_NOT_IMPLEMENTED_YET;
  67. + {
  68. + if (grub_file_seek (hdr, 0) == (grub_off_t) -1)
  69. + return grub_errno;
  70. +
  71. + if (grub_file_read (hdr, &header, sizeof (header)) != sizeof (header))
  72. + return grub_errno;
  73. + }
  74. + else
  75. + err = grub_disk_read (source, 0, 0, sizeof (header), &header);
  76. - err = grub_disk_read (source, 0, 0, sizeof (header), &header);
  77. if (err)
  78. return err;
  79. @@ -240,13 +254,19 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr)
  80. return grub_crypto_gcry_error (gcry_err);
  81. }
  82. + sector = grub_be_to_cpu32 (header.keyblock[i].keyMaterialOffset);
  83. length = (keysize * grub_be_to_cpu32 (header.keyblock[i].stripes));
  84. /* Read and decrypt the key material from the disk. */
  85. - err = grub_disk_read (source,
  86. - grub_be_to_cpu32 (header.keyblock
  87. - [i].keyMaterialOffset), 0,
  88. - length, split_key);
  89. + if (hdr)
  90. + {
  91. + if (grub_file_seek (hdr, sector * 512) == (grub_off_t) -1)
  92. + return grub_errno;
  93. + if (grub_file_read (hdr, split_key, length) != (grub_ssize_t)length)
  94. + return grub_errno;
  95. + }
  96. + else
  97. + err = grub_disk_read (source, sector, 0, length, split_key);
  98. if (err)
  99. {
  100. grub_free (split_key);
  101. --
  102. 2.28.0