3
0

luks.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * volume_id - reads filesystem label and uuid
  3. *
  4. * Copyright (C) 2005 W. Michael Petullo <mike@flyn.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_LUKS) += luks.o
  21. //config:
  22. //config:config FEATURE_VOLUMEID_LUKS
  23. //config: bool "luks filesystem"
  24. //config: default y
  25. //config: depends on VOLUMEID
  26. //config: help
  27. //config: TODO
  28. //config:
  29. #include "volume_id_internal.h"
  30. #define LUKS_MAGIC_L 6
  31. #define UUID_STRING_L 40
  32. #define LUKS_CIPHERNAME_L 32
  33. #define LUKS_CIPHERMODE_L 32
  34. #define LUKS_HASHSPEC_L 32
  35. #define LUKS_DIGESTSIZE 20
  36. #define LUKS_SALTSIZE 32
  37. #define LUKS_NUMKEYS 8
  38. static const uint8_t LUKS_MAGIC[] ALIGN1 = { 'L','U','K','S', 0xba, 0xbe };
  39. struct luks_phdr {
  40. uint8_t magic[LUKS_MAGIC_L];
  41. uint16_t version;
  42. uint8_t cipherName[LUKS_CIPHERNAME_L];
  43. uint8_t cipherMode[LUKS_CIPHERMODE_L];
  44. uint8_t hashSpec[LUKS_HASHSPEC_L];
  45. uint32_t payloadOffset;
  46. uint32_t keyBytes;
  47. uint8_t mkDigest[LUKS_DIGESTSIZE];
  48. uint8_t mkDigestSalt[LUKS_SALTSIZE];
  49. uint32_t mkDigestIterations;
  50. uint8_t uuid[UUID_STRING_L];
  51. struct {
  52. uint32_t active;
  53. uint32_t passwordIterations;
  54. uint8_t passwordSalt[LUKS_SALTSIZE];
  55. uint32_t keyMaterialOffset;
  56. uint32_t stripes;
  57. } keyblock[LUKS_NUMKEYS];
  58. };
  59. enum {
  60. EXPECTED_SIZE_luks_phdr = 0
  61. + 1 * LUKS_MAGIC_L
  62. + 2
  63. + 1 * LUKS_CIPHERNAME_L
  64. + 1 * LUKS_CIPHERMODE_L
  65. + 1 * LUKS_HASHSPEC_L
  66. + 4
  67. + 4
  68. + 1 * LUKS_DIGESTSIZE
  69. + 1 * LUKS_SALTSIZE
  70. + 4
  71. + 1 * UUID_STRING_L
  72. + LUKS_NUMKEYS * (0
  73. + 4
  74. + 4
  75. + 1 * LUKS_SALTSIZE
  76. + 4
  77. + 4
  78. )
  79. };
  80. struct BUG_bad_size_luks_phdr {
  81. char BUG_bad_size_luks_phdr[
  82. sizeof(struct luks_phdr) == EXPECTED_SIZE_luks_phdr ?
  83. 1 : -1];
  84. };
  85. int FAST_FUNC volume_id_probe_luks(struct volume_id *id /*,uint64_t off*/)
  86. {
  87. #define off ((uint64_t)0)
  88. struct luks_phdr *header;
  89. header = volume_id_get_buffer(id, off, sizeof(*header));
  90. if (header == NULL)
  91. return -1;
  92. if (memcmp(header->magic, LUKS_MAGIC, LUKS_MAGIC_L))
  93. return -1;
  94. // volume_id_set_usage(id, VOLUME_ID_CRYPTO);
  95. volume_id_set_uuid(id, header->uuid, UUID_DCE_STRING);
  96. IF_FEATURE_BLKID_TYPE(id->type = "crypto_LUKS";)
  97. return 0;
  98. }