v6-0006-cryptodisk-Add-support-for-LUKS1-key-files.patch 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. From 6251a7f5223b61605a9b4ba11bb18ed607c05f50 Mon Sep 17 00:00:00 2001
  2. From: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
  3. Date: Tue, 17 Mar 2020 08:02:10 +0100
  4. Subject: [PATCH v6 6/6] cryptodisk: Add support for LUKS1 key files
  5. cryptsetup supports key files thourh the --key-file
  6. --header command line argument for both LUKS1 and LUKS2.
  7. This adds support for LUKS1 key files.
  8. Signed-off-by: John Lane <john@lane.uk.net>
  9. GNUtoo@cyberdimension.org: rebase, 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 | 43 ++++++++++++++++++++++++++-----------------
  14. 1 file changed, 26 insertions(+), 17 deletions(-)
  15. diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c
  16. index 0dd33b2af..125a21902 100644
  17. --- a/grub-core/disk/luks.c
  18. +++ b/grub-core/disk/luks.c
  19. @@ -167,7 +167,9 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr,
  20. struct grub_luks_phdr header;
  21. grub_size_t keysize;
  22. grub_uint8_t *split_key = NULL;
  23. - char passphrase[MAX_PASSPHRASE] = "";
  24. + char interactive_passphrase[MAX_PASSPHRASE] = "";
  25. + grub_uint8_t *passphrase;
  26. + grub_size_t passphrase_length;
  27. grub_uint8_t candidate_digest[sizeof (header.mkDigest)];
  28. unsigned i;
  29. grub_size_t length;
  30. @@ -176,10 +178,6 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr,
  31. char *tmp;
  32. grub_uint32_t sector;
  33. - /* Keyfiles are not implemented yet */
  34. - if (keyfile_bytes || keyfile_bytes_size)
  35. - return GRUB_ERR_NOT_IMPLEMENTED_YET;
  36. -
  37. if (hdr)
  38. {
  39. if (grub_file_seek (hdr, 0) == (grub_off_t) -1)
  40. @@ -208,18 +206,29 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr,
  41. if (!split_key)
  42. return grub_errno;
  43. - /* Get the passphrase from the user. */
  44. - tmp = NULL;
  45. - if (source->partition)
  46. - tmp = grub_partition_get_name (source->partition);
  47. - grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
  48. - source->partition ? "," : "", tmp ? : "",
  49. - dev->uuid);
  50. - grub_free (tmp);
  51. - if (!grub_password_get (passphrase, MAX_PASSPHRASE))
  52. + if (keyfile_bytes)
  53. {
  54. - grub_free (split_key);
  55. - return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied");
  56. + /* Use bytestring from key file as passphrase */
  57. + passphrase = keyfile_bytes;
  58. + passphrase_length = keyfile_bytes_size;
  59. + }
  60. + else
  61. + {
  62. + /* Get the passphrase from the user. */
  63. + tmp = NULL;
  64. + if (source->partition)
  65. + tmp = grub_partition_get_name (source->partition);
  66. + grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
  67. + source->partition ? "," : "", tmp ? : "", dev->uuid);
  68. + grub_free (tmp);
  69. + if (!grub_password_get (interactive_passphrase, MAX_PASSPHRASE))
  70. + {
  71. + grub_free (split_key);
  72. + return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied");
  73. + }
  74. +
  75. + passphrase = (grub_uint8_t *)interactive_passphrase;
  76. + passphrase_length = grub_strlen (interactive_passphrase);
  77. }
  78. /* Try to recover master key from each active keyslot. */
  79. @@ -237,7 +246,7 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr,
  80. /* Calculate the PBKDF2 of the user supplied passphrase. */
  81. gcry_err = grub_crypto_pbkdf2 (dev->hash, (grub_uint8_t *) passphrase,
  82. - grub_strlen (passphrase),
  83. + passphrase_length,
  84. header.keyblock[i].passwordSalt,
  85. sizeof (header.keyblock[i].passwordSalt),
  86. grub_be_to_cpu32 (header.keyblock[i].
  87. --
  88. 2.28.0