chiark / gitweb /
[PATCH] udev_volume_id: volume_id v35
[elogind.git] / extras / volume_id / volume_id / luks.c
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
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <netinet/in.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <asm/types.h>
37
38 #include "volume_id.h"
39 #include "util.h"
40 #include "logging.h"
41 #include "luks.h"
42
43 /* from cryptsetup-luks internal.h */
44 #define SECTOR_SHIFT            9
45 #define SECTOR_SIZE             (1 << SECTOR_SHIFT)
46
47 /* from cryptsetup-luks luks.h */
48 #define LUKS_CIPHERNAME_L 32
49 #define LUKS_CIPHERMODE_L 32
50 #define LUKS_HASHSPEC_L 32
51 #define LUKS_DIGESTSIZE 20 /* since SHA1 */
52 #define LUKS_SALTSIZE 32
53 #define LUKS_NUMKEYS 8
54
55 /* from cryptsetup-luks luks.h */
56 const unsigned char LUKS_MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
57 #define LUKS_MAGIC_L 6
58
59 /* from cryptsetup-luks luks.h */
60 #define LUKS_PHDR_SIZE (sizeof(struct luks_phdr)/SECTOR_SIZE+1)
61
62 /* from cryptsetup-luks luks.h */
63 #define UUID_STRING_L 40
64
65 int volume_id_probe_luks(struct volume_id *id, __u64 off)
66 {
67         /* from cryptsetup-luks luks.h */
68         struct luks_phdr {
69                 char            magic[LUKS_MAGIC_L];
70                 uint16_t        version;
71                 char            cipherName[LUKS_CIPHERNAME_L];
72                 char            cipherMode[LUKS_CIPHERMODE_L];
73                 char            hashSpec[LUKS_HASHSPEC_L];
74                 uint32_t        payloadOffset;
75                 uint32_t        keyBytes;
76                 char            mkDigest[LUKS_DIGESTSIZE];
77                 char            mkDigestSalt[LUKS_SALTSIZE];
78                 uint32_t        mkDigestIterations;
79                 char            uuid[UUID_STRING_L];
80                 struct {
81                         uint32_t active;
82
83                         /* parameters used for password processing */
84                         uint32_t passwordIterations;
85                         char     passwordSalt[LUKS_SALTSIZE];
86
87                         /* parameters used for AF store/load */
88                         uint32_t keyMaterialOffset;
89                         uint32_t stripes;
90                 } keyblock[LUKS_NUMKEYS];
91         } *header;
92
93         header = (struct luks_phdr*) volume_id_get_buffer(id, off, LUKS_PHDR_SIZE);
94
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);
103         id->type = "crypto_LUKS";
104
105         return 0;
106 }