chiark / gitweb /
ff90f5d3d195d2a52e43e2d0cd6358e88edefe71
[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 program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "volume_id.h"
27 #include "util.h"
28 #include "logging.h"
29 #include "luks.h"
30
31 #define SECTOR_SHIFT                    9
32 #define SECTOR_SIZE                     (1 << SECTOR_SHIFT)
33
34 #define LUKS_CIPHERNAME_L               32
35 #define LUKS_CIPHERMODE_L               32
36 #define LUKS_HASHSPEC_L                 32
37 #define LUKS_DIGESTSIZE                 20
38 #define LUKS_SALTSIZE                   32
39 #define LUKS_NUMKEYS                    8
40
41 const uint8_t LUKS_MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
42 #define LUKS_MAGIC_L 6
43 #define LUKS_PHDR_SIZE (sizeof(struct luks_phdr)/SECTOR_SIZE+1)
44 #define UUID_STRING_L 40
45
46 struct luks_phdr {
47         uint8_t         magic[LUKS_MAGIC_L];
48         uint16_t        version;
49         uint8_t         cipherName[LUKS_CIPHERNAME_L];
50         uint8_t         cipherMode[LUKS_CIPHERMODE_L];
51         uint8_t         hashSpec[LUKS_HASHSPEC_L];
52         uint32_t        payloadOffset;
53         uint32_t        keyBytes;
54         uint8_t         mkDigest[LUKS_DIGESTSIZE];
55         uint8_t         mkDigestSalt[LUKS_SALTSIZE];
56         uint32_t        mkDigestIterations;
57         uint8_t         uuid[UUID_STRING_L];
58         struct {
59                 uint32_t        active;
60                 uint32_t        passwordIterations;
61                 uint8_t         passwordSalt[LUKS_SALTSIZE];
62                 uint32_t        keyMaterialOffset;
63                 uint32_t        stripes;
64         } keyblock[LUKS_NUMKEYS];
65 };
66
67 int volume_id_probe_luks(struct volume_id *id, uint64_t off)
68 {
69         struct luks_phdr *header;
70
71         header = (struct luks_phdr*) volume_id_get_buffer(id, off, LUKS_PHDR_SIZE);
72         if (header == NULL)
73                 return -1;
74
75         if (memcmp(header->magic, LUKS_MAGIC, LUKS_MAGIC_L))
76                 return -1;
77
78         volume_id_set_usage(id, VOLUME_ID_CRYPTO);
79         volume_id_set_uuid(id, header->uuid, UUID_DCE_STRING);
80
81         id->type = "crypto_LUKS";
82
83         return 0;
84 }