Fixed the build error for riscv64 arch using gcc 13
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_kernel.c
1 /*
2  * Linux kernel userspace API crypto backend implementation
3  *
4  * Copyright (C) 2010-2021 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2010-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 <errno.h>
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <sys/utsname.h>
29 #include <linux/if_alg.h>
30 #include "crypto_backend_internal.h"
31
32 /* FIXME: remove later */
33 #ifndef AF_ALG
34 #define AF_ALG 38
35 #endif
36 #ifndef SOL_ALG
37 #define SOL_ALG 279
38 #endif
39
40 static int crypto_backend_initialised = 0;
41 static char version[256];
42
43 struct hash_alg {
44         const char *name;
45         const char *kernel_name;
46         int length;
47         unsigned int block_length;
48 };
49
50 static struct hash_alg hash_algs[] = {
51         { "sha1",      "sha1",        20,  64 },
52         { "sha224",    "sha224",      28,  64 },
53         { "sha256",    "sha256",      32,  64 },
54         { "sha384",    "sha384",      48, 128 },
55         { "sha512",    "sha512",      64, 128 },
56         { "ripemd160", "rmd160",      20,  64 },
57         { "whirlpool", "wp512",       64,  64 },
58         { "sha3-224",  "sha3-224",    28, 144 },
59         { "sha3-256",  "sha3-256",    32, 136 },
60         { "sha3-384",  "sha3-384",    48, 104 },
61         { "sha3-512",  "sha3-512",    64,  72 },
62         { "stribog256","streebog256", 32,  64 },
63         { "stribog512","streebog512", 64,  64 },
64         { "sm3",       "sm3",         32,  64 },
65         { "blake2b-160","blake2b-160",20, 128 },
66         { "blake2b-256","blake2b-256",32, 128 },
67         { "blake2b-384","blake2b-384",48, 128 },
68         { "blake2b-512","blake2b-512",64, 128 },
69         { "blake2s-128","blake2s-128",16,  64 },
70         { "blake2s-160","blake2s-160",20,  64 },
71         { "blake2s-224","blake2s-224",28,  64 },
72         { "blake2s-256","blake2s-256",32,  64 },
73         { NULL,        NULL,           0,   0 }
74 };
75
76 struct crypt_hash {
77         int tfmfd;
78         int opfd;
79         int hash_len;
80 };
81
82 struct crypt_hmac {
83         int tfmfd;
84         int opfd;
85         int hash_len;
86 };
87
88 struct crypt_cipher {
89         struct crypt_cipher_kernel ck;
90 };
91
92 static int crypt_kernel_socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd,
93                                     const void *key, size_t key_length)
94 {
95         *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
96         if (*tfmfd < 0)
97                 return -ENOTSUP;
98
99         if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) < 0) {
100                 close(*tfmfd);
101                 *tfmfd = -1;
102                 return -ENOENT;
103         }
104
105         if (key && setsockopt(*tfmfd, SOL_ALG, ALG_SET_KEY, key, key_length) < 0) {
106                 close(*tfmfd);
107                 *tfmfd = -1;
108                 return -EINVAL;
109         }
110
111         *opfd = accept(*tfmfd, NULL, 0);
112         if (*opfd < 0) {
113                 close(*tfmfd);
114                 *tfmfd = -1;
115                 return -EINVAL;
116         }
117
118         return 0;
119 }
120
121 int crypt_backend_init(void)
122 {
123         struct utsname uts;
124         struct sockaddr_alg sa = {
125                 .salg_family = AF_ALG,
126                 .salg_type = "hash",
127                 .salg_name = "sha256",
128         };
129         int r, tfmfd = -1, opfd = -1;
130
131         if (crypto_backend_initialised)
132                 return 0;
133
134         if (uname(&uts) == -1 || strcmp(uts.sysname, "Linux"))
135                 return -EINVAL;
136
137         r = snprintf(version, sizeof(version), "%s %s kernel cryptoAPI",
138                 uts.sysname, uts.release);
139         if (r < 0 || (size_t)r >= sizeof(version))
140                 return -EINVAL;
141
142         if (crypt_kernel_socket_init(&sa, &tfmfd, &opfd, NULL, 0) < 0)
143                 return -EINVAL;
144
145         close(tfmfd);
146         close(opfd);
147
148         crypto_backend_initialised = 1;
149         return 0;
150 }
151
152 void crypt_backend_destroy(void)
153 {
154         crypto_backend_initialised = 0;
155 }
156
157 uint32_t crypt_backend_flags(void)
158 {
159         return CRYPT_BACKEND_KERNEL;
160 }
161
162 const char *crypt_backend_version(void)
163 {
164         return crypto_backend_initialised ? version : "";
165 }
166
167 static struct hash_alg *_get_alg(const char *name)
168 {
169         int i = 0;
170
171         while (name && hash_algs[i].name) {
172                 if (!strcmp(name, hash_algs[i].name))
173                         return &hash_algs[i];
174                 i++;
175         }
176         return NULL;
177 }
178
179 /* HASH */
180 int crypt_hash_size(const char *name)
181 {
182         struct hash_alg *ha = _get_alg(name);
183
184         return ha ? ha->length : -EINVAL;
185 }
186
187 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
188 {
189         struct crypt_hash *h;
190         struct hash_alg *ha;
191         struct sockaddr_alg sa = {
192                 .salg_family = AF_ALG,
193                 .salg_type = "hash",
194         };
195
196         h = malloc(sizeof(*h));
197         if (!h)
198                 return -ENOMEM;
199
200         ha = _get_alg(name);
201         if (!ha) {
202                 free(h);
203                 return -EINVAL;
204         }
205         h->hash_len = ha->length;
206
207         strncpy((char *)sa.salg_name, ha->kernel_name, sizeof(sa.salg_name)-1);
208
209         if (crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd, NULL, 0) < 0) {
210                 free(h);
211                 return -EINVAL;
212         }
213
214         *ctx = h;
215         return 0;
216 }
217
218 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
219 {
220         ssize_t r;
221
222         r = send(ctx->opfd, buffer, length, MSG_MORE);
223         if (r < 0 || (size_t)r < length)
224                 return -EIO;
225
226         return 0;
227 }
228
229 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
230 {
231         ssize_t r;
232
233         if (length > (size_t)ctx->hash_len)
234                 return -EINVAL;
235
236         r = read(ctx->opfd, buffer, length);
237         if (r < 0)
238                 return -EIO;
239
240         return 0;
241 }
242
243 void crypt_hash_destroy(struct crypt_hash *ctx)
244 {
245         if (ctx->tfmfd >= 0)
246                 close(ctx->tfmfd);
247         if (ctx->opfd >= 0)
248                 close(ctx->opfd);
249         memset(ctx, 0, sizeof(*ctx));
250         free(ctx);
251 }
252
253 /* HMAC */
254 int crypt_hmac_size(const char *name)
255 {
256         return crypt_hash_size(name);
257 }
258
259 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
260                     const void *key, size_t key_length)
261 {
262         struct crypt_hmac *h;
263         struct hash_alg *ha;
264         struct sockaddr_alg sa = {
265                 .salg_family = AF_ALG,
266                 .salg_type = "hash",
267         };
268         int r;
269
270         h = malloc(sizeof(*h));
271         if (!h)
272                 return -ENOMEM;
273
274         ha = _get_alg(name);
275         if (!ha) {
276                 free(h);
277                 return -EINVAL;
278         }
279         h->hash_len = ha->length;
280
281         r = snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
282                  "hmac(%s)", ha->kernel_name);
283         if (r < 0 || (size_t)r >= sizeof(sa.salg_name)) {
284                 free(h);
285                 return -EINVAL;
286         }
287
288         if (crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd, key, key_length) < 0) {
289                 free(h);
290                 return -EINVAL;
291         }
292
293         *ctx = h;
294         return 0;
295 }
296
297 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
298 {
299         ssize_t r;
300
301         r = send(ctx->opfd, buffer, length, MSG_MORE);
302         if (r < 0 || (size_t)r < length)
303                 return -EIO;
304
305         return 0;
306 }
307
308 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
309 {
310         ssize_t r;
311
312         if (length > (size_t)ctx->hash_len)
313                 return -EINVAL;
314
315         r = read(ctx->opfd, buffer, length);
316         if (r < 0)
317                 return -EIO;
318
319         return 0;
320 }
321
322 void crypt_hmac_destroy(struct crypt_hmac *ctx)
323 {
324         if (ctx->tfmfd >= 0)
325                 close(ctx->tfmfd);
326         if (ctx->opfd >= 0)
327                 close(ctx->opfd);
328         memset(ctx, 0, sizeof(*ctx));
329         free(ctx);
330 }
331
332 /* RNG - N/A */
333 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
334 {
335         return -EINVAL;
336 }
337
338 /* PBKDF */
339 int crypt_pbkdf(const char *kdf, const char *hash,
340                 const char *password, size_t password_length,
341                 const char *salt, size_t salt_length,
342                 char *key, size_t key_length,
343                 uint32_t iterations, uint32_t memory, uint32_t parallel)
344 {
345         struct hash_alg *ha;
346
347         if (!kdf)
348                 return -EINVAL;
349
350         if (!strcmp(kdf, "pbkdf2")) {
351                 ha = _get_alg(hash);
352                 if (!ha)
353                         return -EINVAL;
354
355                 return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
356                                     iterations, key_length, key, ha->block_length);
357         } else if (!strncmp(kdf, "argon2", 6)) {
358                 return argon2(kdf, password, password_length, salt, salt_length,
359                               key, key_length, iterations, memory, parallel);
360         }
361
362         return -EINVAL;
363 }
364
365 /* Block ciphers */
366 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
367                     const char *mode, const void *key, size_t key_length)
368 {
369         struct crypt_cipher *h;
370         int r;
371
372         h = malloc(sizeof(*h));
373         if (!h)
374                 return -ENOMEM;
375
376         r = crypt_cipher_init_kernel(&h->ck, name, mode, key, key_length);
377         if (r < 0) {
378                 free(h);
379                 return r;
380         }
381
382         *ctx = h;
383         return 0;
384 }
385
386 void crypt_cipher_destroy(struct crypt_cipher *ctx)
387 {
388         crypt_cipher_destroy_kernel(&ctx->ck);
389         free(ctx);
390 }
391
392 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
393                          const char *in, char *out, size_t length,
394                          const char *iv, size_t iv_length)
395 {
396         return crypt_cipher_encrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
397 }
398
399 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
400                          const char *in, char *out, size_t length,
401                          const char *iv, size_t iv_length)
402 {
403         return crypt_cipher_decrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
404 }
405
406 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
407 {
408         return true;
409 }
410
411 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
412                             const char *in, char *out, size_t length,
413                             const char *iv, size_t iv_length,
414                             const char *tag, size_t tag_length)
415 {
416         return crypt_bitlk_decrypt_key_kernel(key, key_length, in, out, length,
417                                               iv, iv_length, tag, tag_length);
418 }