chiark / gitweb /
libudev: libudev.pc add Libs.private
[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 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 "libvolume_id.h"
27 #include "libvolume_id-private.h"
28 #include "util.h"
29
30 #define SECTOR_SHIFT                    9
31 #define SECTOR_SIZE                     (1 << SECTOR_SHIFT)
32
33 #define LUKS_CIPHERNAME_L               32
34 #define LUKS_CIPHERMODE_L               32
35 #define LUKS_HASHSPEC_L                 32
36 #define LUKS_DIGESTSIZE                 20
37 #define LUKS_SALTSIZE                   32
38 #define LUKS_NUMKEYS                    8
39
40 #define LUKS_MAGIC_L                    6
41 #define LUKS_PHDR_SIZE                  (sizeof(struct luks_phdr)/SECTOR_SIZE+1)
42 #define UUID_STRING_L                   40
43 static const uint8_t LUKS_MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
44
45 struct luks_phdr {
46         uint8_t         magic[LUKS_MAGIC_L];
47         uint16_t        version;
48         uint8_t         cipherName[LUKS_CIPHERNAME_L];
49         uint8_t         cipherMode[LUKS_CIPHERMODE_L];
50         uint8_t         hashSpec[LUKS_HASHSPEC_L];
51         uint32_t        payloadOffset;
52         uint32_t        keyBytes;
53         uint8_t         mkDigest[LUKS_DIGESTSIZE];
54         uint8_t         mkDigestSalt[LUKS_SALTSIZE];
55         uint32_t        mkDigestIterations;
56         uint8_t         uuid[UUID_STRING_L];
57         struct {
58                 uint32_t        active;
59                 uint32_t        passwordIterations;
60                 uint8_t         passwordSalt[LUKS_SALTSIZE];
61                 uint32_t        keyMaterialOffset;
62                 uint32_t        stripes;
63         } keyblock[LUKS_NUMKEYS];
64 };
65
66 int volume_id_probe_luks(struct volume_id *id, uint64_t off, uint64_t size)
67 {
68         struct luks_phdr *header;
69
70         header = (struct luks_phdr*) volume_id_get_buffer(id, off, LUKS_PHDR_SIZE);
71         if (header == NULL)
72                 return -1;
73
74         if (memcmp(header->magic, LUKS_MAGIC, LUKS_MAGIC_L))
75                 return -1;
76
77         volume_id_set_usage(id, VOLUME_ID_CRYPTO);
78         volume_id_set_uuid(id, header->uuid, 36, UUID_HEX_STRING);
79         snprintf(id->type_version, sizeof(header->version), "%u", le16_to_cpu(header->version));
80         id->type = "crypto_LUKS";
81         return 0;
82 }