c93f4715e027ba8944e588c34b612cbf64068ec1
[platform/upstream/cryptsetup.git] / lib / luks1 / keyencryption.c
1 /*
2  * LUKS - Linux Unified Key Setup
3  *
4  * Copyright (C) 2004-2006, Clemens Fruhwirth <clemens@endorphin.org>
5  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include "internal.h"
25
26 static void _error_hint(struct crypt_device *ctx, const char *device,
27                         const char *cipher_spec, const char *mode, size_t keyLength)
28 {
29         log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
30                         "Check that kernel supports %s cipher (check syslog for more info).\n"),
31                         device, cipher_spec);
32
33         if (!strncmp(mode, "xts", 3) && (keyLength != 256 && keyLength != 512))
34                 log_err(ctx, _("Key size in XTS mode must be 256 or 512 bits.\n"));
35 }
36
37 static int LUKS_endec_template(char *src, size_t srcLength,
38                                const char *cipher, const char *cipher_mode,
39                                struct volume_key *vk,
40                                unsigned int sector,
41                                ssize_t (*func)(int, int, void *, size_t),
42                                int mode,
43                                struct crypt_device *ctx)
44 {
45         char name[PATH_MAX], path[PATH_MAX];
46         char cipher_spec[MAX_CIPHER_LEN * 3];
47         struct crypt_dm_active_device dmd = {
48                 .target = DM_CRYPT,
49                 .uuid   = NULL,
50                 .flags  = CRYPT_ACTIVATE_PRIVATE,
51                 .data_device = crypt_metadata_device(ctx),
52                 .u.crypt = {
53                         .cipher = cipher_spec,
54                         .vk     = vk,
55                         .offset = sector,
56                         .iv_offset = 0,
57                 }
58         };
59         int r, bsize, devfd = -1;
60
61         bsize = device_block_size(dmd.data_device);
62         if (bsize <= 0)
63                 return -EINVAL;
64
65         dmd.size = size_round_up(srcLength, bsize) / SECTOR_SIZE;
66
67         if (mode == O_RDONLY)
68                 dmd.flags |= CRYPT_ACTIVATE_READONLY;
69
70         if (snprintf(name, sizeof(name), "temporary-cryptsetup-%d", getpid()) < 0)
71                 return -ENOMEM;
72         if (snprintf(path, sizeof(path), "%s/%s", dm_get_dir(), name) < 0)
73                 return -ENOMEM;
74         if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode) < 0)
75                 return -ENOMEM;
76
77         r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
78                                 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
79         if (r < 0) {
80                 log_err(ctx, _("Device %s doesn't exist or access denied.\n"),
81                         device_path(dmd.data_device));
82                 return -EIO;
83         }
84
85         if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
86                 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
87                         device_path(dmd.data_device));
88                 return -EACCES;
89         }
90
91         r = dm_create_device(ctx, name, "TEMP", &dmd, 0);
92         if (r < 0) {
93                 if (r != -EACCES && r != -ENOTSUP)
94                         _error_hint(ctx, device_path(dmd.data_device),
95                                     cipher_spec, cipher_mode, vk->keylength * 8);
96                 return -EIO;
97         }
98
99         devfd = open(path, mode | O_DIRECT | O_SYNC);
100         if (devfd == -1) {
101                 log_err(ctx, _("Failed to open temporary keystore device.\n"));
102                 r = -EIO;
103                 goto out;
104         }
105
106         r = func(devfd, bsize, src, srcLength);
107         if (r < 0) {
108                 log_err(ctx, _("Failed to access temporary keystore device.\n"));
109                 r = -EIO;
110         } else
111                 r = 0;
112  out:
113         if(devfd != -1)
114                 close(devfd);
115         dm_remove_device(ctx, name, 1, dmd.size);
116         return r;
117 }
118
119 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
120                             const char *cipher,
121                             const char *cipher_mode,
122                             struct volume_key *vk,
123                             unsigned int sector,
124                             struct crypt_device *ctx)
125 {
126         return LUKS_endec_template(src, srcLength, cipher, cipher_mode,
127                                    vk, sector, write_blockwise, O_RDWR, ctx);
128 }
129
130 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
131                               const char *cipher,
132                               const char *cipher_mode,
133                               struct volume_key *vk,
134                               unsigned int sector,
135                               struct crypt_device *ctx)
136 {
137         return LUKS_endec_template(dst, dstLength, cipher, cipher_mode,
138                                    vk, sector, read_blockwise, O_RDONLY, ctx);
139 }