2 * Linux kernel userspace API crypto backend implementation
4 * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-2023 Milan Broz
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.
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.
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.
27 #include <sys/socket.h>
28 #include <sys/utsname.h>
29 #include <linux/if_alg.h>
30 #include "crypto_backend_internal.h"
39 static int crypto_backend_initialised = 0;
40 static char version[256];
44 const char *kernel_name;
46 unsigned int block_length;
49 static struct hash_alg hash_algs[] = {
50 { "sha1", "sha1", 20, 64 },
51 { "sha224", "sha224", 28, 64 },
52 { "sha256", "sha256", 32, 64 },
53 { "sha384", "sha384", 48, 128 },
54 { "sha512", "sha512", 64, 128 },
55 { "ripemd160", "rmd160", 20, 64 },
56 { "whirlpool", "wp512", 64, 64 },
57 { "sha3-224", "sha3-224", 28, 144 },
58 { "sha3-256", "sha3-256", 32, 136 },
59 { "sha3-384", "sha3-384", 48, 104 },
60 { "sha3-512", "sha3-512", 64, 72 },
61 { "stribog256","streebog256", 32, 64 },
62 { "stribog512","streebog512", 64, 64 },
63 { "sm3", "sm3", 32, 64 },
64 { "blake2b-160","blake2b-160",20, 128 },
65 { "blake2b-256","blake2b-256",32, 128 },
66 { "blake2b-384","blake2b-384",48, 128 },
67 { "blake2b-512","blake2b-512",64, 128 },
68 { "blake2s-128","blake2s-128",16, 64 },
69 { "blake2s-160","blake2s-160",20, 64 },
70 { "blake2s-224","blake2s-224",28, 64 },
71 { "blake2s-256","blake2s-256",32, 64 },
88 struct crypt_cipher_kernel ck;
91 static int crypt_kernel_socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd,
92 const void *key, size_t key_length)
94 *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
98 if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) < 0) {
104 if (key && setsockopt(*tfmfd, SOL_ALG, ALG_SET_KEY, key, key_length) < 0) {
110 *opfd = accept(*tfmfd, NULL, 0);
120 int crypt_backend_init(bool fips __attribute__((unused)))
123 struct sockaddr_alg sa = {
124 .salg_family = AF_ALG,
126 .salg_name = "sha256",
128 int r, tfmfd = -1, opfd = -1;
130 if (crypto_backend_initialised)
133 if (uname(&uts) == -1 || strcmp(uts.sysname, "Linux"))
136 r = snprintf(version, sizeof(version), "%s %s kernel cryptoAPI",
137 uts.sysname, uts.release);
138 if (r < 0 || (size_t)r >= sizeof(version))
141 if (crypt_kernel_socket_init(&sa, &tfmfd, &opfd, NULL, 0) < 0)
147 crypto_backend_initialised = 1;
151 void crypt_backend_destroy(void)
153 crypto_backend_initialised = 0;
156 uint32_t crypt_backend_flags(void)
158 return CRYPT_BACKEND_KERNEL;
161 const char *crypt_backend_version(void)
163 return crypto_backend_initialised ? version : "";
166 static struct hash_alg *_get_alg(const char *name)
170 while (name && hash_algs[i].name) {
171 if (!strcmp(name, hash_algs[i].name))
172 return &hash_algs[i];
179 int crypt_hash_size(const char *name)
181 struct hash_alg *ha = _get_alg(name);
183 return ha ? ha->length : -EINVAL;
186 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
188 struct crypt_hash *h;
190 struct sockaddr_alg sa = {
191 .salg_family = AF_ALG,
195 h = malloc(sizeof(*h));
204 h->hash_len = ha->length;
206 strncpy((char *)sa.salg_name, ha->kernel_name, sizeof(sa.salg_name)-1);
208 if (crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd, NULL, 0) < 0) {
217 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
221 r = send(ctx->opfd, buffer, length, MSG_MORE);
222 if (r < 0 || (size_t)r < length)
228 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
232 if (length > (size_t)ctx->hash_len)
235 r = read(ctx->opfd, buffer, length);
242 void crypt_hash_destroy(struct crypt_hash *ctx)
248 memset(ctx, 0, sizeof(*ctx));
253 int crypt_hmac_size(const char *name)
255 return crypt_hash_size(name);
258 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
259 const void *key, size_t key_length)
261 struct crypt_hmac *h;
263 struct sockaddr_alg sa = {
264 .salg_family = AF_ALG,
269 h = malloc(sizeof(*h));
278 h->hash_len = ha->length;
280 r = snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
281 "hmac(%s)", ha->kernel_name);
282 if (r < 0 || (size_t)r >= sizeof(sa.salg_name)) {
287 if (crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd, key, key_length) < 0) {
296 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
300 r = send(ctx->opfd, buffer, length, MSG_MORE);
301 if (r < 0 || (size_t)r < length)
307 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
311 if (length > (size_t)ctx->hash_len)
314 r = read(ctx->opfd, buffer, length);
321 void crypt_hmac_destroy(struct crypt_hmac *ctx)
327 memset(ctx, 0, sizeof(*ctx));
332 int crypt_backend_rng(char *buffer __attribute__((unused)), size_t length __attribute__((unused)),
333 int quality __attribute__((unused)), int fips __attribute__((unused)))
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)
350 if (!strcmp(kdf, "pbkdf2")) {
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);
366 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
367 const char *mode, const void *key, size_t key_length)
369 struct crypt_cipher *h;
372 h = malloc(sizeof(*h));
376 r = crypt_cipher_init_kernel(&h->ck, name, mode, key, key_length);
386 void crypt_cipher_destroy(struct crypt_cipher *ctx)
388 crypt_cipher_destroy_kernel(&ctx->ck);
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)
396 return crypt_cipher_encrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
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)
403 return crypt_cipher_decrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
406 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx __attribute__((unused)))
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)
416 return crypt_bitlk_decrypt_key_kernel(key, key_length, in, out, length,
417 iv, iv_length, tag, tag_length);
420 int crypt_backend_memeq(const void *m1, const void *m2, size_t n)
422 return crypt_internal_memeq(m1, m2, n);
425 bool crypt_fips_mode(void)