| 1 |
/* |
| 2 |
* Copyright (C) 2008 Karel Zak <[email protected]> |
| 3 |
* Copyright (C) 2018-2024 Milan Broz <[email protected]> |
| 4 |
* |
| 5 |
* Inspired by libvolume_id by |
| 6 |
* Kay Sievers <[email protected]> |
| 7 |
* |
| 8 |
* This file may be redistributed under the terms of the |
| 9 |
* GNU Lesser General Public License. |
| 10 |
*/ |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <unistd.h> |
| 14 |
#include <string.h> |
| 15 |
#include <errno.h> |
| 16 |
#include <ctype.h> |
| 17 |
#include <stdint.h> |
| 18 |
#include <stdbool.h> |
| 19 |
|
| 20 |
#include "superblocks.h" |
| 21 |
|
| 22 |
#define LUKS_CIPHERNAME_L 32 |
| 23 |
#define LUKS_CIPHERMODE_L 32 |
| 24 |
#define LUKS_HASHSPEC_L 32 |
| 25 |
#define LUKS_DIGESTSIZE 20 |
| 26 |
#define LUKS_SALTSIZE 32 |
| 27 |
#define LUKS_MAGIC_L 6 |
| 28 |
#define UUID_STRING_L 40 |
| 29 |
#define LUKS2_LABEL_L 48 |
| 30 |
#define LUKS2_SALT_L 64 |
| 31 |
#define LUKS2_CHECKSUM_ALG_L 32 |
| 32 |
#define LUKS2_CHECKSUM_L 64 |
| 33 |
|
| 34 |
#define LUKS_MAGIC "LUKS\xba\xbe" |
| 35 |
#define LUKS_MAGIC_2 "SKUL\xba\xbe" |
| 36 |
|
| 37 |
#define LUKS2_HW_OPAL_SUBSYSTEM "HW-OPAL" |
| 38 |
|
| 39 |
/* Offsets for secondary header (for scan if primary header is corrupted). */ |
| 40 |
#define LUKS2_HDR2_OFFSETS { 0x04000, 0x008000, 0x010000, 0x020000, \ |
| 41 |
0x40000, 0x080000, 0x100000, 0x200000, 0x400000 } |
| 42 |
|
| 43 |
static const uint64_t secondary_offsets[] = LUKS2_HDR2_OFFSETS; |
| 44 |
|
| 45 |
struct luks_phdr { |
| 46 |
uint8_t magic[LUKS_MAGIC_L]; |
| 47 |
uint16_t version; |
| 48 |
uint8_t cipherName[LUKS_CIPHERNAME_L]; |
| 49 |
uint8_t cipherMode[LUKS_CIPHERMODE_L]; |
| 50 |
uint8_t hashSpec[LUKS_HASHSPEC_L]; |
| 51 |
uint32_t payloadOffset; |
| 52 |
uint32_t keyBytes; |
| 53 |
uint8_t mkDigest[LUKS_DIGESTSIZE]; |
| 54 |
uint8_t mkDigestSalt[LUKS_SALTSIZE]; |
| 55 |
uint32_t mkDigestIterations; |
| 56 |
uint8_t uuid[UUID_STRING_L]; |
| 57 |
} __attribute__((packed)); |
| 58 |
|
| 59 |
struct luks2_phdr { |
| 60 |
char magic[LUKS_MAGIC_L]; |
| 61 |
uint16_t version; |
| 62 |
uint64_t hdr_size; /* in bytes, including JSON area */ |
| 63 |
uint64_t seqid; /* increased on every update */ |
| 64 |
char label[LUKS2_LABEL_L]; |
| 65 |
char checksum_alg[LUKS2_CHECKSUM_ALG_L]; |
| 66 |
uint8_t salt[LUKS2_SALT_L]; /* unique for every header/offset */ |
| 67 |
char uuid[UUID_STRING_L]; |
| 68 |
char subsystem[LUKS2_LABEL_L]; /* owner subsystem label */ |
| 69 |
uint64_t hdr_offset; /* offset from device start in bytes */ |
| 70 |
char _padding[184]; |
| 71 |
uint8_t csum[LUKS2_CHECKSUM_L]; |
| 72 |
/* Padding to 4k, then JSON area */ |
| 73 |
} __attribute__ ((packed)); |
| 74 |
|
| 75 |
static int luks_attributes(blkid_probe pr, struct luks2_phdr *header, uint64_t offset) |
| 76 |
{ |
| 77 |
int version; |
| 78 |
struct luks_phdr *header_v1; |
| 79 |
|
| 80 |
if (blkid_probe_set_magic(pr, offset, LUKS_MAGIC_L, (unsigned char *) &header->magic)) |
| 81 |
return BLKID_PROBE_NONE; |
| 82 |
|
| 83 |
version = be16_to_cpu(header->version); |
| 84 |
blkid_probe_sprintf_version(pr, "%u", version); |
| 85 |
|
| 86 |
if (version == 1) { |
| 87 |
header_v1 = (struct luks_phdr *)header; |
| 88 |
blkid_probe_strncpy_uuid(pr, |
| 89 |
(unsigned char *) header_v1->uuid, UUID_STRING_L); |
| 90 |
} else if (version == 2) { |
| 91 |
blkid_probe_strncpy_uuid(pr, |
| 92 |
(unsigned char *) header->uuid, UUID_STRING_L); |
| 93 |
blkid_probe_set_label(pr, |
| 94 |
(unsigned char *) header->label, LUKS2_LABEL_L); |
| 95 |
blkid_probe_set_id_label(pr, "SUBSYSTEM", |
| 96 |
(unsigned char *) header->subsystem, LUKS2_LABEL_L); |
| 97 |
} |
| 98 |
|
| 99 |
return BLKID_PROBE_OK; |
| 100 |
} |
| 101 |
|
| 102 |
static bool luks_valid(struct luks2_phdr *header, const char *magic, uint64_t offset) |
| 103 |
{ |
| 104 |
if (memcmp(header->magic, magic, LUKS_MAGIC_L)) |
| 105 |
return false; |
| 106 |
|
| 107 |
/* LUKS2 header is not at expected offset */ |
| 108 |
if (be16_to_cpu(header->version) == 2 && |
| 109 |
be64_to_cpu(header->hdr_offset) != offset) |
| 110 |
return false; |
| 111 |
|
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
static int probe_luks(blkid_probe pr, const struct blkid_idmag *mag __attribute__((__unused__))) |
| 116 |
{ |
| 117 |
struct luks2_phdr *header; |
| 118 |
size_t i; |
| 119 |
|
| 120 |
header = (struct luks2_phdr *) blkid_probe_get_buffer(pr, 0, sizeof(struct luks2_phdr)); |
| 121 |
if (!header) |
| 122 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 123 |
|
| 124 |
if (luks_valid(header, LUKS_MAGIC, 0)) { |
| 125 |
/* LUKS primary header was found. */ |
| 126 |
return luks_attributes(pr, header, 0); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
/* No primary header, scan for known offsets of LUKS2 secondary header unless this is a safeprobe */ |
| 131 |
if (!(pr->prob_flags & BLKID_PROBE_FL_SAFEPROBE)) { |
| 132 |
for (i = 0; i < ARRAY_SIZE(secondary_offsets); i++) { |
| 133 |
header = (struct luks2_phdr *) blkid_probe_get_buffer(pr, |
| 134 |
secondary_offsets[i], sizeof(struct luks2_phdr)); |
| 135 |
|
| 136 |
if (!header) |
| 137 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 138 |
|
| 139 |
if (luks_valid(header, LUKS_MAGIC_2, secondary_offsets[i])) |
| 140 |
return luks_attributes(pr, header, secondary_offsets[i]); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return BLKID_PROBE_NONE; |
| 145 |
} |
| 146 |
|
| 147 |
static int probe_luks_opal(blkid_probe pr, const struct blkid_idmag *mag __attribute__((__unused__))) |
| 148 |
{ |
| 149 |
struct luks2_phdr *header; |
| 150 |
int version; |
| 151 |
|
| 152 |
header = (struct luks2_phdr *) blkid_probe_get_buffer(pr, 0, sizeof(struct luks2_phdr)); |
| 153 |
if (!header) |
| 154 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 155 |
|
| 156 |
if (!luks_valid(header, LUKS_MAGIC, 0)) |
| 157 |
return BLKID_PROBE_NONE; |
| 158 |
|
| 159 |
version = be16_to_cpu(header->version); |
| 160 |
|
| 161 |
if (version != 2) |
| 162 |
return BLKID_PROBE_NONE; |
| 163 |
|
| 164 |
if (memcmp(header->subsystem, LUKS2_HW_OPAL_SUBSYSTEM, sizeof(LUKS2_HW_OPAL_SUBSYSTEM)) != 0) |
| 165 |
return BLKID_PROBE_NONE; |
| 166 |
|
| 167 |
if (!blkdid_probe_is_opal_locked(pr)) |
| 168 |
return BLKID_PROBE_NONE; |
| 169 |
|
| 170 |
/* Locked drive with LUKS2 HW OPAL encryption, finish probe now */ |
| 171 |
return luks_attributes(pr, header, 0); |
| 172 |
} |
| 173 |
|
| 174 |
const struct blkid_idinfo luks_idinfo = |
| 175 |
{ |
| 176 |
.name = "crypto_LUKS", |
| 177 |
.usage = BLKID_USAGE_CRYPTO, |
| 178 |
.probefunc = probe_luks, |
| 179 |
.magics = BLKID_NONE_MAGIC |
| 180 |
}; |
| 181 |
|
| 182 |
const struct blkid_idinfo luks_opal_idinfo = |
| 183 |
{ |
| 184 |
.name = "crypto_LUKS", |
| 185 |
.usage = BLKID_USAGE_CRYPTO, |
| 186 |
.probefunc = probe_luks_opal, |
| 187 |
.magics = BLKID_NONE_MAGIC, |
| 188 |
}; |