2 * Linux kernel userspace API crypto backend implementation
4 * Copyright (C) 2010-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-2021 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"
32 /* FIXME: remove later */
40 static int crypto_backend_initialised = 0;
41 static char version[256];
45 const char *kernel_name;
47 unsigned int block_length;
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 },
89 struct crypt_cipher_kernel ck;
92 static int crypt_kernel_socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd,
93 const void *key, size_t key_length)
95 *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
99 if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) < 0) {
105 if (key && setsockopt(*tfmfd, SOL_ALG, ALG_SET_KEY, key, key_length) < 0) {
111 *opfd = accept(*tfmfd, NULL, 0);
121 int crypt_backend_init(void)
124 struct sockaddr_alg sa = {
125 .salg_family = AF_ALG,
127 .salg_name = "sha256",
129 int r, tfmfd = -1, opfd = -1;
131 if (crypto_backend_initialised)
134 if (uname(&uts) == -1 || strcmp(uts.sysname, "Linux"))
137 r = snprintf(version, sizeof(version), "%s %s kernel cryptoAPI",
138 uts.sysname, uts.release);
139 if (r < 0 || (size_t)r >= sizeof(version))
142 if (crypt_kernel_socket_init(&sa, &tfmfd, &opfd, NULL, 0) < 0)
148 crypto_backend_initialised = 1;
152 void crypt_backend_destroy(void)
154 crypto_backend_initialised = 0;
157 uint32_t crypt_backend_flags(void)
159 return CRYPT_BACKEND_KERNEL;
162 const char *crypt_backend_version(void)
164 return crypto_backend_initialised ? version : "";
167 static struct hash_alg *_get_alg(const char *name)
171 while (name && hash_algs[i].name) {
172 if (!strcmp(name, hash_algs[i].name))
173 return &hash_algs[i];
180 int crypt_hash_size(const char *name)
182 struct hash_alg *ha = _get_alg(name);
184 return ha ? ha->length : -EINVAL;
187 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
189 struct crypt_hash *h;
191 struct sockaddr_alg sa = {
192 .salg_family = AF_ALG,
196 h = malloc(sizeof(*h));
205 h->hash_len = ha->length;
207 strncpy((char *)sa.salg_name, ha->kernel_name, sizeof(sa.salg_name)-1);
209 if (crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd, NULL, 0) < 0) {
218 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
222 r = send(ctx->opfd, buffer, length, MSG_MORE);
223 if (r < 0 || (size_t)r < length)
229 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
233 if (length > (size_t)ctx->hash_len)
236 r = read(ctx->opfd, buffer, length);
243 void crypt_hash_destroy(struct crypt_hash *ctx)
249 memset(ctx, 0, sizeof(*ctx));
254 int crypt_hmac_size(const char *name)
256 return crypt_hash_size(name);
259 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
260 const void *key, size_t key_length)
262 struct crypt_hmac *h;
264 struct sockaddr_alg sa = {
265 .salg_family = AF_ALG,
270 h = malloc(sizeof(*h));
279 h->hash_len = ha->length;
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)) {
288 if (crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd, key, key_length) < 0) {
297 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
301 r = send(ctx->opfd, buffer, length, MSG_MORE);
302 if (r < 0 || (size_t)r < length)
308 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
312 if (length > (size_t)ctx->hash_len)
315 r = read(ctx->opfd, buffer, length);
322 void crypt_hmac_destroy(struct crypt_hmac *ctx)
328 memset(ctx, 0, sizeof(*ctx));
333 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
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)
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);