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