Change License from GPLv2 only to GPLv2+ ("or any later").
[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  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include "internal.h"
26
27 static void _error_hint(struct crypt_device *ctx, const char *device,
28                         const char *cipher_spec, const char *mode, size_t keyLength)
29 {
30         log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
31                         "Check that kernel supports %s cipher (check syslog for more info).\n"),
32                         device, cipher_spec);
33
34         if (!strncmp(mode, "xts", 3) && (keyLength != 256 && keyLength != 512))
35                 log_err(ctx, _("Key size in XTS mode must be 256 or 512 bits.\n"));
36 }
37
38 static int LUKS_endec_template(char *src, size_t srcLength,
39                                const char *cipher, const char *cipher_mode,
40                                struct volume_key *vk,
41                                unsigned int sector,
42                                ssize_t (*func)(int, int, void *, size_t),
43                                int mode,
44                                struct crypt_device *ctx)
45 {
46         char name[PATH_MAX], path[PATH_MAX];
47         char cipher_spec[MAX_CIPHER_LEN * 3];
48         struct crypt_dm_active_device dmd = {
49                 .target = DM_CRYPT,
50                 .uuid   = NULL,
51                 .flags  = CRYPT_ACTIVATE_PRIVATE,
52                 .data_device = crypt_metadata_device(ctx),
53                 .u.crypt = {
54                         .cipher = cipher_spec,
55                         .vk     = vk,
56                         .offset = sector,
57                         .iv_offset = 0,
58                 }
59         };
60         int r, bsize, devfd = -1;
61
62         bsize = device_block_size(dmd.data_device);
63         if (bsize <= 0)
64                 return -EINVAL;
65
66         dmd.size = size_round_up(srcLength, bsize) / SECTOR_SIZE;
67
68         if (mode == O_RDONLY)
69                 dmd.flags |= CRYPT_ACTIVATE_READONLY;
70
71         if (snprintf(name, sizeof(name), "temporary-cryptsetup-%d", getpid()) < 0)
72                 return -ENOMEM;
73         if (snprintf(path, sizeof(path), "%s/%s", dm_get_dir(), name) < 0)
74                 return -ENOMEM;
75         if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode) < 0)
76                 return -ENOMEM;
77
78         r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
79                                 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
80         if (r < 0) {
81                 log_err(ctx, _("Device %s doesn't exist or access denied.\n"),
82                         device_path(dmd.data_device));
83                 return -EIO;
84         }
85
86         if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
87                 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
88                         device_path(dmd.data_device));
89                 return -EACCES;
90         }
91
92         r = dm_create_device(ctx, name, "TEMP", &dmd, 0);
93         if (r < 0) {
94                 if (r != -EACCES && r != -ENOTSUP)
95                         _error_hint(ctx, device_path(dmd.data_device),
96                                     cipher_spec, cipher_mode, vk->keylength * 8);
97                 return -EIO;
98         }
99
100         devfd = open(path, mode | O_DIRECT | O_SYNC);
101         if (devfd == -1) {
102                 log_err(ctx, _("Failed to open temporary keystore device.\n"));
103                 r = -EIO;
104                 goto out;
105         }
106
107         r = func(devfd, bsize, src, srcLength);
108         if (r < 0) {
109                 log_err(ctx, _("Failed to access temporary keystore device.\n"));
110                 r = -EIO;
111         } else
112                 r = 0;
113  out:
114         if(devfd != -1)
115                 close(devfd);
116         dm_remove_device(ctx, name, 1, dmd.size);
117         return r;
118 }
119
120 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
121                             const char *cipher,
122                             const char *cipher_mode,
123                             struct volume_key *vk,
124                             unsigned int sector,
125                             struct crypt_device *ctx)
126 {
127         return LUKS_endec_template(src, srcLength, cipher, cipher_mode,
128                                    vk, sector, write_blockwise, O_RDWR, ctx);
129 }
130
131 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
132                               const char *cipher,
133                               const char *cipher_mode,
134                               struct volume_key *vk,
135                               unsigned int sector,
136                               struct crypt_device *ctx)
137 {
138         return LUKS_endec_template(dst, dstLength, cipher, cipher_mode,
139                                    vk, sector, read_blockwise, O_RDONLY, ctx);
140 }