2 * Linux kernel userspace API crypto backend implementation
4 * Copyright (C) 2010-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-2020 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 },
81 struct crypt_cipher_kernel ck;
84 static int crypt_kernel_socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd,
85 const void *key, size_t key_length)
87 *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
91 if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) < 0) {
97 if (key && setsockopt(*tfmfd, SOL_ALG, ALG_SET_KEY, key, key_length) < 0) {
103 *opfd = accept(*tfmfd, NULL, 0);
113 int crypt_backend_init(void)
116 struct sockaddr_alg sa = {
117 .salg_family = AF_ALG,
119 .salg_name = "sha256",
121 int tfmfd = -1, opfd = -1;
123 if (crypto_backend_initialised)
126 if (uname(&uts) == -1 || strcmp(uts.sysname, "Linux"))
129 if (crypt_kernel_socket_init(&sa, &tfmfd, &opfd, NULL, 0) < 0)
135 snprintf(version, sizeof(version), "%s %s kernel cryptoAPI",
136 uts.sysname, uts.release);
138 crypto_backend_initialised = 1;
142 void crypt_backend_destroy(void)
144 crypto_backend_initialised = 0;
147 uint32_t crypt_backend_flags(void)
149 return CRYPT_BACKEND_KERNEL;
152 const char *crypt_backend_version(void)
154 return crypto_backend_initialised ? version : "";
157 static struct hash_alg *_get_alg(const char *name)
161 while (name && hash_algs[i].name) {
162 if (!strcmp(name, hash_algs[i].name))
163 return &hash_algs[i];
170 int crypt_hash_size(const char *name)
172 struct hash_alg *ha = _get_alg(name);
174 return ha ? ha->length : -EINVAL;
177 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
179 struct crypt_hash *h;
181 struct sockaddr_alg sa = {
182 .salg_family = AF_ALG,
186 h = malloc(sizeof(*h));
195 h->hash_len = ha->length;
197 strncpy((char *)sa.salg_name, ha->kernel_name, sizeof(sa.salg_name)-1);
199 if (crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd, NULL, 0) < 0) {
208 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
212 r = send(ctx->opfd, buffer, length, MSG_MORE);
213 if (r < 0 || (size_t)r < length)
219 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
223 if (length > (size_t)ctx->hash_len)
226 r = read(ctx->opfd, buffer, length);
233 void crypt_hash_destroy(struct crypt_hash *ctx)
239 memset(ctx, 0, sizeof(*ctx));
244 int crypt_hmac_size(const char *name)
246 return crypt_hash_size(name);
249 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
250 const void *key, size_t key_length)
252 struct crypt_hmac *h;
254 struct sockaddr_alg sa = {
255 .salg_family = AF_ALG,
259 h = malloc(sizeof(*h));
268 h->hash_len = ha->length;
270 snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
271 "hmac(%s)", ha->kernel_name);
273 if (crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd, key, key_length) < 0) {
282 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
286 r = send(ctx->opfd, buffer, length, MSG_MORE);
287 if (r < 0 || (size_t)r < length)
293 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
297 if (length > (size_t)ctx->hash_len)
300 r = read(ctx->opfd, buffer, length);
307 void crypt_hmac_destroy(struct crypt_hmac *ctx)
313 memset(ctx, 0, sizeof(*ctx));
318 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
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)
335 if (!strcmp(kdf, "pbkdf2")) {
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);
351 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
352 const char *mode, const void *key, size_t key_length)
354 struct crypt_cipher *h;
357 h = malloc(sizeof(*h));
361 r = crypt_cipher_init_kernel(&h->ck, name, mode, key, key_length);
371 void crypt_cipher_destroy(struct crypt_cipher *ctx)
373 crypt_cipher_destroy_kernel(&ctx->ck);
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)
381 return crypt_cipher_encrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
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)
388 return crypt_cipher_decrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
391 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
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)
401 return crypt_bitlk_decrypt_key_kernel(key, key_length, in, out, length,
402 iv, iv_length, tag, tag_length);