chiark / gitweb /
[PATCH] udev_volume_id: volume_id v38
[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 <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <asm/types.h>
36
37 #include "volume_id.h"
38 #include "util.h"
39 #include "logging.h"
40 #include "luks.h"
41
42 #define SECTOR_SHIFT                    9
43 #define SECTOR_SIZE                     (1 << SECTOR_SHIFT)
44
45 #define LUKS_CIPHERNAME_L               32
46 #define LUKS_CIPHERMODE_L               32
47 #define LUKS_HASHSPEC_L                 32
48 #define LUKS_DIGESTSIZE                 20
49 #define LUKS_SALTSIZE                   32
50 #define LUKS_NUMKEYS                    8
51
52 const __u8 LUKS_MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
53 #define LUKS_MAGIC_L 6
54 #define LUKS_PHDR_SIZE (sizeof(struct luks_phdr)/SECTOR_SIZE+1)
55 #define UUID_STRING_L 40
56
57 struct luks_phdr {
58         __u8            magic[LUKS_MAGIC_L];
59         __u16           version;
60         __u8            cipherName[LUKS_CIPHERNAME_L];
61         __u8            cipherMode[LUKS_CIPHERMODE_L];
62         __u8            hashSpec[LUKS_HASHSPEC_L];
63         __u32           payloadOffset;
64         __u32           keyBytes;
65         __u8            mkDigest[LUKS_DIGESTSIZE];
66         __u8            mkDigestSalt[LUKS_SALTSIZE];
67         __u32           mkDigestIterations;
68         __u8            uuid[UUID_STRING_L];
69         struct {
70                 __u32   active;
71                 __u32   passwordIterations;
72                 __u8            passwordSalt[LUKS_SALTSIZE];
73                 __u32   keyMaterialOffset;
74                 __u32   stripes;
75         } keyblock[LUKS_NUMKEYS];
76 };
77
78 int volume_id_probe_luks(struct volume_id *id, __u64 off)
79 {
80         struct luks_phdr *header;
81
82         header = (struct luks_phdr*) volume_id_get_buffer(id, off, LUKS_PHDR_SIZE);
83         if (header == NULL)
84                 return -1;
85
86         if (memcmp(header->magic, LUKS_MAGIC, LUKS_MAGIC_L))
87                 return -1;
88
89         volume_id_set_usage(id, VOLUME_ID_CRYPTO);
90         volume_id_set_uuid(id, header->uuid, UUID_DCE_STRING);
91
92         id->type = "crypto_LUKS";
93
94         return 0;
95 }