Support test run in kernel FIPS mode.
[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 "luks.h"
26 #include "internal.h"
27
28 static void _error_hint(struct crypt_device *ctx, const char *device,
29                         const char *cipher_spec, const char *mode, size_t keyLength)
30 {
31         log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
32                         "Check that kernel supports %s cipher (check syslog for more info).\n"),
33                         device, cipher_spec);
34
35         if (!strncmp(mode, "xts", 3) && (keyLength != 256 && keyLength != 512))
36                 log_err(ctx, _("Key size in XTS mode must be 256 or 512 bits.\n"));
37 }
38
39 static int LUKS_endec_template(char *src, size_t srcLength,
40                                const char *cipher, const char *cipher_mode,
41                                struct volume_key *vk,
42                                unsigned int sector,
43                                ssize_t (*func)(int, int, void *, size_t),
44                                int mode,
45                                struct crypt_device *ctx)
46 {
47         char name[PATH_MAX], path[PATH_MAX];
48         char cipher_spec[MAX_CIPHER_LEN * 3];
49         struct crypt_dm_active_device dmd = {
50                 .target = DM_CRYPT,
51                 .uuid   = NULL,
52                 .flags  = CRYPT_ACTIVATE_PRIVATE,
53                 .data_device = crypt_metadata_device(ctx),
54                 .u.crypt = {
55                         .cipher = cipher_spec,
56                         .vk     = vk,
57                         .offset = sector,
58                         .iv_offset = 0,
59                 }
60         };
61         int r, bsize, devfd = -1;
62
63         bsize = device_block_size(dmd.data_device);
64         if (bsize <= 0)
65                 return -EINVAL;
66
67         dmd.size = size_round_up(srcLength, bsize) / SECTOR_SIZE;
68
69         if (mode == O_RDONLY)
70                 dmd.flags |= CRYPT_ACTIVATE_READONLY;
71
72         if (snprintf(name, sizeof(name), "temporary-cryptsetup-%d", getpid()) < 0)
73                 return -ENOMEM;
74         if (snprintf(path, sizeof(path), "%s/%s", dm_get_dir(), name) < 0)
75                 return -ENOMEM;
76         if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode) < 0)
77                 return -ENOMEM;
78
79         r = device_block_adjust(ctx, dmd.data_device, DEV_OK,
80                                 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
81         if (r < 0) {
82                 log_err(ctx, _("Device %s doesn't exist or access denied.\n"),
83                         device_path(dmd.data_device));
84                 return -EIO;
85         }
86
87         if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
88                 log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
89                         device_path(dmd.data_device));
90                 return -EACCES;
91         }
92
93         r = dm_create_device(ctx, name, "TEMP", &dmd, 0);
94         if (r < 0) {
95                 if (r != -EACCES && r != -ENOTSUP)
96                         _error_hint(ctx, device_path(dmd.data_device),
97                                     cipher_spec, cipher_mode, vk->keylength * 8);
98                 return -EIO;
99         }
100
101         devfd = open(path, mode | O_DIRECT | O_SYNC);
102         if (devfd == -1) {
103                 log_err(ctx, _("Failed to open temporary keystore device.\n"));
104                 r = -EIO;
105                 goto out;
106         }
107
108         r = func(devfd, bsize, src, srcLength);
109         if (r < 0) {
110                 log_err(ctx, _("Failed to access temporary keystore device.\n"));
111                 r = -EIO;
112         } else
113                 r = 0;
114  out:
115         if(devfd != -1)
116                 close(devfd);
117         dm_remove_device(ctx, name, 1, dmd.size);
118         return r;
119 }
120
121 int LUKS_encrypt_to_storage(char *src, size_t srcLength,
122                             const char *cipher,
123                             const char *cipher_mode,
124                             struct volume_key *vk,
125                             unsigned int sector,
126                             struct crypt_device *ctx)
127 {
128         return LUKS_endec_template(src, srcLength, cipher, cipher_mode,
129                                    vk, sector, write_blockwise, O_RDWR, ctx);
130 }
131
132 int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
133                               const char *cipher,
134                               const char *cipher_mode,
135                               struct volume_key *vk,
136                               unsigned int sector,
137                               struct crypt_device *ctx)
138 {
139         return LUKS_endec_template(dst, dstLength, cipher, cipher_mode,
140                                    vk, sector, read_blockwise, O_RDONLY, ctx);
141 }