import source from lvm2 2.02.79
[external/device-mapper.git] / lib / device / dev-luks.c
1 /*
2  * Copyright (C) 2010 Red Hat, Inc. All rights reserved.
3  *
4  * This file is part of LVM2.
5  *
6  * This copyrighted material is made available to anyone wishing to use,
7  * modify, copy, or redistribute it subject to the terms and conditions
8  * of the GNU Lesser General Public License v.2.1.
9  *
10  * You should have received a copy of the GNU Lesser General Public License
11  * along with this program; if not, write to the Free Software Foundation,
12  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
13  */
14
15 #include "lib.h"
16 #include "metadata.h"
17
18 #define LUKS_SIGNATURE "LUKS\xba\xbe"
19 #define LUKS_SIGNATURE_SIZE 6
20
21 int dev_is_luks(struct device *dev, uint64_t *signature)
22 {
23         char buf[LUKS_SIGNATURE_SIZE];
24         int ret = -1;
25
26         if (!dev_open(dev)) {
27                 stack;
28                 return -1;
29         }
30
31         *signature = 0;
32
33         if (!dev_read(dev, 0, LUKS_SIGNATURE_SIZE, buf))
34                 goto_out;
35
36         ret = memcmp(buf, LUKS_SIGNATURE, LUKS_SIGNATURE_SIZE) ? 0 : 1;
37
38 out:
39         if (!dev_close(dev))
40                 stack;
41
42         return ret;
43 }