Fixed the build error for riscv64 arch using gcc 13
[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-2021 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2012-2021 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         int r;
100
101         if (!strcmp(name, "cipher_null"))
102                 key_length = 0;
103
104         r = snprintf((char *)sa.salg_name, sizeof(sa.salg_name), "%s(%s)", mode, name);
105         if (r < 0 || (size_t)r >= sizeof(sa.salg_name))
106                 return -EINVAL;
107
108         return _crypt_cipher_init(ctx, key, key_length, 0, &sa);
109 }
110
111 /* The in/out should be aligned to page boundary */
112 static int _crypt_cipher_crypt(struct crypt_cipher_kernel *ctx,
113                                const char *in, size_t in_length,
114                                char *out, size_t out_length,
115                                const char *iv, size_t iv_length,
116                                uint32_t direction)
117 {
118         int r = 0;
119         ssize_t len;
120         struct af_alg_iv *alg_iv;
121         struct cmsghdr *header;
122         uint32_t *type;
123         struct iovec iov = {
124                 .iov_base = (void*)(uintptr_t)in,
125                 .iov_len = in_length,
126         };
127         int iv_msg_size = iv ? CMSG_SPACE(sizeof(*alg_iv) + iv_length) : 0;
128         char buffer[CMSG_SPACE(sizeof(*type)) + iv_msg_size];
129         struct msghdr msg = {
130                 .msg_control = buffer,
131                 .msg_controllen = sizeof(buffer),
132                 .msg_iov = &iov,
133                 .msg_iovlen = 1,
134         };
135
136         if (!in || !out || !in_length)
137                 return -EINVAL;
138
139         if ((!iv && iv_length) || (iv && !iv_length))
140                 return -EINVAL;
141
142         memset(buffer, 0, sizeof(buffer));
143
144         /* Set encrypt/decrypt operation */
145         header = CMSG_FIRSTHDR(&msg);
146         if (!header)
147                 return -EINVAL;
148
149         header->cmsg_level = SOL_ALG;
150         header->cmsg_type = ALG_SET_OP;
151         header->cmsg_len = CMSG_LEN(sizeof(*type));
152         type = (void*)CMSG_DATA(header);
153         *type = direction;
154
155         /* Set IV */
156         if (iv) {
157                 header = CMSG_NXTHDR(&msg, header);
158                 if (!header)
159                         return -EINVAL;
160
161                 header->cmsg_level = SOL_ALG;
162                 header->cmsg_type = ALG_SET_IV;
163                 header->cmsg_len = iv_msg_size;
164                 alg_iv = (void*)CMSG_DATA(header);
165                 alg_iv->ivlen = iv_length;
166                 memcpy(alg_iv->iv, iv, iv_length);
167         }
168
169         len = sendmsg(ctx->opfd, &msg, 0);
170         if (len != (ssize_t)(in_length)) {
171                 r = -EIO;
172                 goto bad;
173         }
174
175         len = read(ctx->opfd, out, out_length);
176         if (len != (ssize_t)out_length)
177                 r = -EIO;
178 bad:
179         crypt_backend_memzero(buffer, sizeof(buffer));
180         return r;
181 }
182
183 int crypt_cipher_encrypt_kernel(struct crypt_cipher_kernel *ctx,
184                                 const char *in, char *out, size_t length,
185                                 const char *iv, size_t iv_length)
186 {
187         return _crypt_cipher_crypt(ctx, in, length, out, length,
188                                    iv, iv_length, ALG_OP_ENCRYPT);
189 }
190
191 int crypt_cipher_decrypt_kernel(struct crypt_cipher_kernel *ctx,
192                                 const char *in, char *out, size_t length,
193                                 const char *iv, size_t iv_length)
194 {
195         return _crypt_cipher_crypt(ctx, in, length, out, length,
196                                    iv, iv_length, ALG_OP_DECRYPT);
197 }
198
199 void crypt_cipher_destroy_kernel(struct crypt_cipher_kernel *ctx)
200 {
201         if (ctx->tfmfd >= 0)
202                 close(ctx->tfmfd);
203         if (ctx->opfd >= 0)
204                 close(ctx->opfd);
205
206         ctx->tfmfd = -1;
207         ctx->opfd = -1;
208 }
209
210 int crypt_cipher_check_kernel(const char *name, const char *mode,
211                               const char *integrity, size_t key_length)
212 {
213         struct crypt_cipher_kernel c;
214         char mode_name[64], tmp_salg_name[180], *real_mode = NULL, *cipher_iv = NULL, *key;
215         const char *salg_type;
216         bool aead;
217         int r;
218         struct sockaddr_alg sa = {
219                 .salg_family = AF_ALG,
220         };
221
222         aead = integrity && strcmp(integrity, "none");
223
224         /* Remove IV if present */
225         if (mode) {
226                 strncpy(mode_name, mode, sizeof(mode_name));
227                 mode_name[sizeof(mode_name) - 1] = 0;
228                 cipher_iv = strchr(mode_name, '-');
229                 if (cipher_iv) {
230                         *cipher_iv = '\0';
231                         real_mode = mode_name;
232                 }
233         }
234
235         salg_type = aead ? "aead" : "skcipher";
236         r = snprintf((char *)sa.salg_type, sizeof(sa.salg_type), "%s", salg_type);
237         if (r < 0 || (size_t)r >= sizeof(sa.salg_name))
238                 return -EINVAL;
239
240         memset(tmp_salg_name, 0, sizeof(tmp_salg_name));
241
242         /* FIXME: this is duplicating a part of devmapper backend */
243         if (aead && !strcmp(integrity, "poly1305"))
244                 r = snprintf(tmp_salg_name, sizeof(tmp_salg_name), "rfc7539(%s,%s)", name, integrity);
245         else if (!real_mode)
246                 r = snprintf(tmp_salg_name, sizeof(tmp_salg_name), "%s", name);
247         else if (aead && !strcmp(real_mode, "ccm"))
248                 r = snprintf(tmp_salg_name, sizeof(tmp_salg_name), "rfc4309(%s(%s))", real_mode, name);
249         else
250                 r = snprintf(tmp_salg_name, sizeof(tmp_salg_name), "%s(%s)", real_mode, name);
251
252         if (r < 0 || (size_t)r >= sizeof(tmp_salg_name))
253                 return -EINVAL;
254
255         memcpy(sa.salg_name, tmp_salg_name, sizeof(sa.salg_name));
256
257         key = malloc(key_length);
258         if (!key)
259                 return -ENOMEM;
260
261         /* We cannot use RNG yet, any key works here, tweak the first part if it is split key (XTS). */
262         memset(key, 0xab, key_length);
263         *key = 0xef;
264
265         r = _crypt_cipher_init(&c, key, key_length, 0, &sa);
266         crypt_cipher_destroy_kernel(&c);
267         free(key);
268
269         return r;
270 }
271
272 int crypt_bitlk_decrypt_key_kernel(const void *key, size_t key_length,
273                                    const char *in, char *out, size_t length,
274                                    const char *iv, size_t iv_length,
275                                    const char *tag, size_t tag_length)
276 {
277         struct crypt_cipher_kernel c;
278         struct sockaddr_alg sa = {
279                 .salg_family = AF_ALG,
280                 .salg_type = "aead",
281                 .salg_name = "ccm(aes)",
282         };
283         int r;
284         char buffer[128], ccm_iv[16];
285
286         if (length + tag_length > sizeof(buffer))
287                 return -EINVAL;
288
289         if (iv_length > sizeof(ccm_iv) - 2)
290                 return -EINVAL;
291
292         r = _crypt_cipher_init(&c, key, key_length, tag_length, &sa);
293         if (r < 0)
294                 return r;
295
296         memcpy(buffer, in, length);
297         memcpy(buffer + length, tag, tag_length);
298
299         /* CCM IV - RFC3610 */
300         memset(ccm_iv, 0, sizeof(ccm_iv));
301         ccm_iv[0] = 15 - iv_length - 1;
302         memcpy(ccm_iv + 1, iv, iv_length);
303         memset(ccm_iv + 1 + iv_length, 0, ccm_iv[0] + 1);
304         iv_length = sizeof(ccm_iv);
305
306         r =  _crypt_cipher_crypt(&c, buffer, length + tag_length, out, length,
307                                  ccm_iv, iv_length, ALG_OP_DECRYPT);
308
309         crypt_cipher_destroy_kernel(&c);
310         crypt_backend_memzero(buffer, sizeof(buffer));
311
312         return r;
313 }
314
315 #else /* ENABLE_AF_ALG */
316 int crypt_cipher_init_kernel(struct crypt_cipher_kernel *ctx, const char *name,
317                              const char *mode, const void *key, size_t key_length)
318 {
319         return -ENOTSUP;
320 }
321
322 void crypt_cipher_destroy_kernel(struct crypt_cipher_kernel *ctx)
323 {
324         return;
325 }
326
327 int crypt_cipher_encrypt_kernel(struct crypt_cipher_kernel *ctx,
328                                 const char *in, char *out, size_t length,
329                                 const char *iv, size_t iv_length)
330 {
331         return -EINVAL;
332 }
333 int crypt_cipher_decrypt_kernel(struct crypt_cipher_kernel *ctx,
334                                 const char *in, char *out, size_t length,
335                                 const char *iv, size_t iv_length)
336 {
337         return -EINVAL;
338 }
339 int crypt_cipher_check_kernel(const char *name, const char *mode,
340                               const char *integrity, size_t key_length)
341 {
342         /* Cannot check, expect success. */
343         return 0;
344 }
345 int crypt_bitlk_decrypt_key_kernel(const void *key, size_t key_length,
346                                    const char *in, char *out, size_t length,
347                                    const char *iv, size_t iv_length,
348                                    const char *tag, size_t tag_length)
349 {
350         return -ENOTSUP;
351 }
352 #endif