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