chiark / gitweb /
usb_id: fix switch statement for video type
[elogind.git] / extras / volume_id / lib / 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 program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30
31 #include "libvolume_id.h"
32 #include "libvolume_id-private.h"
33
34 #define SECTOR_SHIFT                    9
35 #define SECTOR_SIZE                     (1 << SECTOR_SHIFT)
36
37 #define LUKS_CIPHERNAME_L               32
38 #define LUKS_CIPHERMODE_L               32
39 #define LUKS_HASHSPEC_L                 32
40 #define LUKS_DIGESTSIZE                 20
41 #define LUKS_SALTSIZE                   32
42 #define LUKS_NUMKEYS                    8
43
44 #define LUKS_MAGIC_L                    6
45 #define LUKS_PHDR_SIZE                  (sizeof(struct luks_phdr)/SECTOR_SIZE+1)
46 #define UUID_STRING_L                   40
47 static const uint8_t LUKS_MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
48
49 struct luks_phdr {
50         uint8_t         magic[LUKS_MAGIC_L];
51         uint16_t        version;
52         uint8_t         cipherName[LUKS_CIPHERNAME_L];
53         uint8_t         cipherMode[LUKS_CIPHERMODE_L];
54         uint8_t         hashSpec[LUKS_HASHSPEC_L];
55         uint32_t        payloadOffset;
56         uint32_t        keyBytes;
57         uint8_t         mkDigest[LUKS_DIGESTSIZE];
58         uint8_t         mkDigestSalt[LUKS_SALTSIZE];
59         uint32_t        mkDigestIterations;
60         uint8_t         uuid[UUID_STRING_L];
61         struct {
62                 uint32_t        active;
63                 uint32_t        passwordIterations;
64                 uint8_t         passwordSalt[LUKS_SALTSIZE];
65                 uint32_t        keyMaterialOffset;
66                 uint32_t        stripes;
67         } keyblock[LUKS_NUMKEYS];
68 };
69
70 int volume_id_probe_luks(struct volume_id *id, uint64_t off, uint64_t size)
71 {
72         struct luks_phdr *header;
73
74         header = (struct luks_phdr*) volume_id_get_buffer(id, off, LUKS_PHDR_SIZE);
75         if (header == NULL)
76                 return -1;
77
78         if (memcmp(header->magic, LUKS_MAGIC, LUKS_MAGIC_L))
79                 return -1;
80
81         volume_id_set_usage(id, VOLUME_ID_CRYPTO);
82         volume_id_set_uuid(id, header->uuid, 36, UUID_HEX_STRING);
83         snprintf(id->type_version, sizeof(header->version), "%u", le16_to_cpu(header->version));
84         id->type = "crypto_LUKS";
85         return 0;
86 }