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 //config:config FEATURE_VOLUMEID_LUKS
21 //config:	bool "luks filesystem"
22 //config:	default y
23 //config:	depends on VOLUMEID
24 
25 //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_LUKS) += luks.o
26 
27 #include "volume_id_internal.h"
28 
29 #define LUKS_MAGIC_L             6
30 #define UUID_STRING_L           40
31 #define LUKS_CIPHERNAME_L       32
32 #define LUKS_CIPHERMODE_L       32
33 #define LUKS_HASHSPEC_L         32
34 #define LUKS_DIGESTSIZE         20
35 #define LUKS_SALTSIZE           32
36 #define LUKS_NUMKEYS             8
37 
38 static const uint8_t LUKS_MAGIC[] ALIGN1 = { 'L','U','K','S', 0xba, 0xbe };
39 
40 struct luks_phdr {
41 	uint8_t		magic[LUKS_MAGIC_L];
42 	uint16_t	version;
43 	uint8_t		cipherName[LUKS_CIPHERNAME_L];
44 	uint8_t		cipherMode[LUKS_CIPHERMODE_L];
45 	uint8_t		hashSpec[LUKS_HASHSPEC_L];
46 	uint32_t	payloadOffset;
47 	uint32_t	keyBytes;
48 	uint8_t		mkDigest[LUKS_DIGESTSIZE];
49 	uint8_t		mkDigestSalt[LUKS_SALTSIZE];
50 	uint32_t	mkDigestIterations;
51 	uint8_t		uuid[UUID_STRING_L];
52 	struct {
53 		uint32_t	active;
54 		uint32_t	passwordIterations;
55 		uint8_t		passwordSalt[LUKS_SALTSIZE];
56 		uint32_t	keyMaterialOffset;
57 		uint32_t	stripes;
58 	} keyblock[LUKS_NUMKEYS];
59 };
60 
61 enum {
62 	EXPECTED_SIZE_luks_phdr = 0
63 		+ 1 * LUKS_MAGIC_L
64 		+ 2
65 		+ 1 * LUKS_CIPHERNAME_L
66 		+ 1 * LUKS_CIPHERMODE_L
67 		+ 1 * LUKS_HASHSPEC_L
68 		+ 4
69 		+ 4
70 		+ 1 * LUKS_DIGESTSIZE
71 		+ 1 * LUKS_SALTSIZE
72 		+ 4
73 		+ 1 * UUID_STRING_L
74 		+ LUKS_NUMKEYS * (0
75 		  + 4
76 		  + 4
77 		  + 1 * LUKS_SALTSIZE
78 		  + 4
79 		  + 4
80 		  )
81 };
82 
83 struct BUG_bad_size_luks_phdr {
84 	char BUG_bad_size_luks_phdr[
85 		sizeof(struct luks_phdr) == EXPECTED_SIZE_luks_phdr ?
86 		1 : -1];
87 };
88 
volume_id_probe_luks(struct volume_id * id)89 int FAST_FUNC volume_id_probe_luks(struct volume_id *id /*,uint64_t off*/)
90 {
91 #define off ((uint64_t)0)
92 	struct luks_phdr *header;
93 
94 	header = volume_id_get_buffer(id, off, sizeof(*header));
95 	if (header == NULL)
96 		return -1;
97 
98 	if (memcmp(header->magic, LUKS_MAGIC, LUKS_MAGIC_L))
99 		return -1;
100 
101 //	volume_id_set_usage(id, VOLUME_ID_CRYPTO);
102 	volume_id_set_uuid(id, header->uuid, UUID_DCE_STRING);
103 	IF_FEATURE_BLKID_TYPE(id->type = "crypto_LUKS";)
104 
105 	return 0;
106 }
107