1a8aecfc8a8b2aa66d5895ef6ce6887fa4a83ffb
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_cipher_kernel.c
1 /*
2  * Linux kernel userspace API crypto backend implementation (skcipher)
3  *
4  * Copyright (C) 2012-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2012-2020 Milan Broz
6  *
7  * This file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This file 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this file; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include "crypto_backend_internal.h"
31
32 #ifdef ENABLE_AF_ALG
33
34 #include <linux/if_alg.h>
35
36 #ifndef AF_ALG
37 #define AF_ALG 38
38 #endif
39 #ifndef SOL_ALG
40 #define SOL_ALG 279
41 #endif
42
43 #ifndef ALG_SET_AEAD_AUTHSIZE
44 #define ALG_SET_AEAD_AUTHSIZE 5
45 #endif
46
47 /*
48  * ciphers
49  *
50  * ENOENT - algorithm not available
51  * ENOTSUP - AF_ALG family not available
52  * (but cannot check specifically for skcipher API)
53  */
54 static int _crypt_cipher_init(struct crypt_cipher_kernel *ctx,
55                               const void *key, size_t key_length,
56                               size_t tag_length, struct sockaddr_alg *sa)
57 {
58         if (!ctx)
59                 return -EINVAL;
60
61         ctx->opfd = -1;
62         ctx->tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
63         if (ctx->tfmfd < 0) {
64                 crypt_cipher_destroy_kernel(ctx);
65                 return -ENOTSUP;
66         }
67
68         if (bind(ctx->tfmfd, (struct sockaddr *)sa, sizeof(*sa)) < 0) {
69                 crypt_cipher_destroy_kernel(ctx);
70                 return -ENOENT;
71         }
72
73         if (setsockopt(ctx->tfmfd, SOL_ALG, ALG_SET_KEY, key, key_length) < 0) {
74                 crypt_cipher_destroy_kernel(ctx);
75                 return -EINVAL;
76         }
77
78         if (tag_length && setsockopt(ctx->tfmfd, SOL_ALG, ALG_SET_AEAD_AUTHSIZE, NULL, tag_length) < 0) {
79                 crypt_cipher_destroy_kernel(ctx);
80                 return -EINVAL;
81         }
82
83         ctx->opfd = accept(ctx->tfmfd, NULL, 0);
84         if (ctx->opfd < 0) {
85                 crypt_cipher_destroy_kernel(ctx);
86                 return -EINVAL;
87         }
88
89         return 0;
90 }
91
92 int crypt_cipher_init_kernel(struct crypt_cipher_kernel *ctx, const char *name,
93                              const char *mode, const void *key, size_t key_length)
94 {
95         struct sockaddr_alg sa = {
96                 .salg_family = AF_ALG,
97                 .salg_type = "skcipher",
98         };
99
100         if (!strcmp(name, "cipher_null"))
101                 key_length = 0;
102
103         snprintf((char *)sa.salg_name, sizeof(sa.salg_name), "%s(%s)", mode, name);
104
105         return _crypt_cipher_init(ctx, key, key_length, 0, &sa);
106 }
107
108 /* The in/out should be aligned to page boundary */
109 static int _crypt_cipher_crypt(struct crypt_cipher_kernel *ctx,
110                                const char *in, size_t in_length,
111                                char *out, size_t out_length,
112                                const char *iv, size_t iv_length,
113                                uint32_t direction)
114 {
115         int r = 0;
116         ssize_t len;
117         struct af_alg_iv *alg_iv;
118         struct cmsghdr *header;
119         uint32_t *type;
120         struct iovec iov = {
121                 .iov_base = (void*)(uintptr_t)in,
122                 .iov_len = in_length,
123         };
124         int iv_msg_size = iv ? CMSG_SPACE(sizeof(*alg_iv) + iv_length) : 0;
125         char buffer[CMSG_SPACE(sizeof(*type)) + iv_msg_size];
126         struct msghdr msg = {
127                 .msg_control = buffer,
128                 .msg_controllen = sizeof(buffer),
129                 .msg_iov = &iov,
130                 .msg_iovlen = 1,
131         };
132
133         if (!in || !out || !in_length)
134                 return -EINVAL;
135
136         if ((!iv && iv_length) || (iv && !iv_length))
137                 return -EINVAL;
138
139         memset(buffer, 0, sizeof(buffer));
140
141         /* Set encrypt/decrypt operation */
142         header = CMSG_FIRSTHDR(&msg);
143         if (!header)
144                 return -EINVAL;
145
146         header->cmsg_level = SOL_ALG;
147         header->cmsg_type = ALG_SET_OP;
148         header->cmsg_len = CMSG_LEN(sizeof(*type));
149         type = (void*)CMSG_DATA(header);
150         *type = direction;
151
152         /* Set IV */
153         if (iv) {
154                 header = CMSG_NXTHDR(&msg, header);
155                 header->cmsg_level = SOL_ALG;
156                 header->cmsg_type = ALG_SET_IV;
157                 header->cmsg_len = iv_msg_size;
158                 alg_iv = (void*)CMSG_DATA(header);
159                 alg_iv->ivlen = iv_length;
160                 memcpy(alg_iv->iv, iv, iv_length);
161         }
162
163         len = sendmsg(ctx->opfd, &msg, 0);
164         if (len != (ssize_t)(in_length)) {
165                 r = -EIO;
166                 goto bad;
167         }
168
169         len = read(ctx->opfd, out, out_length);
170         if (len != (ssize_t)out_length)
171                 r = -EIO;
172 bad:
173         crypt_backend_memzero(buffer, sizeof(buffer));
174         return r;
175 }
176
177 int crypt_cipher_encrypt_kernel(struct crypt_cipher_kernel *ctx,
178                                 const char *in, char *out, size_t length,
179                                 const char *iv, size_t iv_length)
180 {
181         return _crypt_cipher_crypt(ctx, in, length, out, length,
182                                    iv, iv_length, ALG_OP_ENCRYPT);
183 }
184
185 int crypt_cipher_decrypt_kernel(struct crypt_cipher_kernel *ctx,
186                                 const char *in, char *out, size_t length,
187                                 const char *iv, size_t iv_length)
188 {
189         return _crypt_cipher_crypt(ctx, in, length, out, length,
190                                    iv, iv_length, ALG_OP_DECRYPT);
191 }
192
193 void crypt_cipher_destroy_kernel(struct crypt_cipher_kernel *ctx)
194 {
195         if (ctx->tfmfd >= 0)
196                 close(ctx->tfmfd);
197         if (ctx->opfd >= 0)
198                 close(ctx->opfd);
199
200         ctx->tfmfd = -1;
201         ctx->opfd = -1;
202 }
203
204 int crypt_cipher_check_kernel(const char *name, const char *mode,
205                               const char *integrity, size_t key_length)
206 {
207         struct crypt_cipher_kernel c;
208         char mode_name[64], tmp_salg_name[180], *real_mode = NULL, *cipher_iv = NULL, *key;
209         const char *salg_type;
210         bool aead;
211         int r;
212         struct sockaddr_alg sa = {
213                 .salg_family = AF_ALG,
214         };
215
216         aead = integrity && strcmp(integrity, "none");
217
218         /* Remove IV if present */
219         if (mode) {
220                 strncpy(mode_name, mode, sizeof(mode_name));
221                 mode_name[sizeof(mode_name) - 1] = 0;
222                 cipher_iv = strchr(mode_name, '-');
223                 if (cipher_iv) {
224                         *cipher_iv = '\0';
225                         real_mode = mode_name;
226                 }
227         }
228
229         salg_type = aead ? "aead" : "skcipher";
230         snprintf((char *)sa.salg_type, sizeof(sa.salg_type), "%s", salg_type);
231         memset(tmp_salg_name, 0, sizeof(tmp_salg_name));
232
233         /* FIXME: this is duplicating a part of devmapper backend */
234         if (aead && !strcmp(integrity, "poly1305"))
235                 r = snprintf(tmp_salg_name, sizeof(tmp_salg_name), "rfc7539(%s,%s)", name, integrity);
236         else if (!real_mode)
237                 r = snprintf(tmp_salg_name, sizeof(tmp_salg_name), "%s", name);
238         else if (aead && !strcmp(real_mode, "ccm"))
239                 r = snprintf(tmp_salg_name, sizeof(tmp_salg_name), "rfc4309(%s(%s))", real_mode, name);
240         else
241                 r = snprintf(tmp_salg_name, sizeof(tmp_salg_name), "%s(%s)", real_mode, name);
242
243         if (r <= 0 || r > (int)(sizeof(sa.salg_name) - 1))
244                 return -EINVAL;
245
246         memcpy(sa.salg_name, tmp_salg_name, sizeof(sa.salg_name));
247
248         key = malloc(key_length);
249         if (!key)
250                 return -ENOMEM;
251
252         /* We cannot use RNG yet, any key works here, tweak the first part if it is split key (XTS). */
253         memset(key, 0xab, key_length);
254         *key = 0xef;
255
256         r = _crypt_cipher_init(&c, key, key_length, 0, &sa);
257         crypt_cipher_destroy_kernel(&c);
258         free(key);
259
260         return r;
261 }
262
263 int crypt_bitlk_decrypt_key_kernel(const void *key, size_t key_length,
264                                    const char *in, char *out, size_t length,
265                                    const char *iv, size_t iv_length,
266                                    const char *tag, size_t tag_length)
267 {
268         struct crypt_cipher_kernel c;
269         struct sockaddr_alg sa = {
270                 .salg_family = AF_ALG,
271                 .salg_type = "aead",
272                 .salg_name = "ccm(aes)",
273         };
274         int r;
275         char buffer[128], ccm_iv[16];
276
277         if (length + tag_length > sizeof(buffer))
278                 return -EINVAL;
279
280         if (iv_length > sizeof(ccm_iv) - 2)
281                 return -EINVAL;
282
283         r = _crypt_cipher_init(&c, key, key_length, tag_length, &sa);
284         if (r < 0)
285                 return r;
286
287         memcpy(buffer, in, length);
288         memcpy(buffer + length, tag, tag_length);
289
290         /* CCM IV - RFC3610 */
291         memset(ccm_iv, 0, sizeof(ccm_iv));
292         ccm_iv[0] = 15 - iv_length - 1;
293         memcpy(ccm_iv + 1, iv, iv_length);
294         memset(ccm_iv + 1 + iv_length, 0, ccm_iv[0] + 1);
295         iv_length = sizeof(ccm_iv);
296
297         r =  _crypt_cipher_crypt(&c, buffer, length + tag_length, out, length,
298                                  ccm_iv, iv_length, ALG_OP_DECRYPT);
299
300         crypt_cipher_destroy_kernel(&c);
301         crypt_backend_memzero(buffer, sizeof(buffer));
302
303         return r;
304 }
305
306 #else /* ENABLE_AF_ALG */
307 int crypt_cipher_init_kernel(struct crypt_cipher_kernel *ctx, const char *name,
308                              const char *mode, const void *key, size_t key_length)
309 {
310         return -ENOTSUP;
311 }
312
313 void crypt_cipher_destroy_kernel(struct crypt_cipher_kernel *ctx)
314 {
315         return;
316 }
317
318 int crypt_cipher_encrypt_kernel(struct crypt_cipher_kernel *ctx,
319                                 const char *in, char *out, size_t length,
320                                 const char *iv, size_t iv_length)
321 {
322         return -EINVAL;
323 }
324 int crypt_cipher_decrypt_kernel(struct crypt_cipher_kernel *ctx,
325                                 const char *in, char *out, size_t length,
326                                 const char *iv, size_t iv_length)
327 {
328         return -EINVAL;
329 }
330 int crypt_cipher_check_kernel(const char *name, const char *mode,
331                               const char *integrity, size_t key_length)
332 {
333         /* Cannot check, expect success. */
334         return 0;
335 }
336 int crypt_bitlk_decrypt_key_kernel(const void *key, size_t key_length,
337                                    const char *in, char *out, size_t length,
338                                    const char *iv, size_t iv_length,
339                                    const char *tag, size_t tag_length)
340 {
341         return -ENOTSUP;
342 }
343 #endif