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