2 * Linux kernel userspace API crypto backend implementation
4 * Copyright (C) 2010-2012, Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/socket.h>
26 #include <sys/utsname.h>
27 #include <linux/if_alg.h>
28 #include "crypto_backend.h"
30 /* FIXME: remove later */
38 static int crypto_backend_initialised = 0;
39 static char version[64];
43 const char *kernel_name;
47 static struct hash_alg hash_algs[] = {
48 { "sha1", "sha1", 20 },
49 { "sha256", "sha256", 32 },
50 { "sha512", "sha512", 64 },
51 { "ripemd160", "rmd160", 20 },
52 { "whirlpool", "wp512", 64 },
68 static int _socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd)
70 *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
74 if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) == -1)
77 *opfd = accept(*tfmfd, NULL, 0);
94 int crypt_backend_init(struct crypt_device *ctx)
97 struct sockaddr_alg sa = {
98 .salg_family = AF_ALG,
102 int tfmfd = -1, opfd = -1;
104 if (crypto_backend_initialised)
107 if (uname(&uts) == -1 || strcmp(uts.sysname, "Linux"))
110 if (_socket_init(&sa, &tfmfd, &opfd) < 0)
116 snprintf(version, sizeof(version), "%s %s kernel cryptoAPI",
117 uts.sysname, uts.release);
119 crypto_backend_initialised = 1;
123 uint32_t crypt_backend_flags(void)
125 return CRYPT_BACKEND_KERNEL;
128 const char *crypt_backend_version(void)
130 return crypto_backend_initialised ? version : "";
133 static struct hash_alg *_get_alg(const char *name)
137 while (name && hash_algs[i].name) {
138 if (!strcmp(name, hash_algs[i].name))
139 return &hash_algs[i];
146 int crypt_hash_size(const char *name)
148 struct hash_alg *ha = _get_alg(name);
150 return ha ? ha->length : -EINVAL;
153 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
155 struct crypt_hash *h;
157 struct sockaddr_alg sa = {
158 .salg_family = AF_ALG,
162 h = malloc(sizeof(*h));
171 h->hash_len = ha->length;
173 strncpy((char *)sa.salg_name, ha->kernel_name, sizeof(sa.salg_name));
175 if (_socket_init(&sa, &h->tfmfd, &h->opfd) < 0) {
184 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
188 r = send(ctx->opfd, buffer, length, MSG_MORE);
189 if (r < 0 || (size_t)r < length)
195 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
199 if (length > (size_t)ctx->hash_len)
202 r = read(ctx->opfd, buffer, length);
209 int crypt_hash_destroy(struct crypt_hash *ctx)
211 if (ctx->tfmfd != -1)
215 memset(ctx, 0, sizeof(*ctx));
221 int crypt_hmac_size(const char *name)
223 return crypt_hash_size(name);
226 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
227 const void *buffer, size_t length)
229 struct crypt_hmac *h;
231 struct sockaddr_alg sa = {
232 .salg_family = AF_ALG,
236 h = malloc(sizeof(*h));
245 h->hash_len = ha->length;
247 snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
248 "hmac(%s)", ha->kernel_name);
250 if (_socket_init(&sa, &h->tfmfd, &h->opfd) < 0) {
255 if (setsockopt(h->tfmfd, SOL_ALG, ALG_SET_KEY, buffer, length) == -1) {
256 crypt_hmac_destroy(h);
264 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
268 r = send(ctx->opfd, buffer, length, MSG_MORE);
269 if (r < 0 || (size_t)r < length)
275 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
279 if (length > (size_t)ctx->hash_len)
282 r = read(ctx->opfd, buffer, length);
289 int crypt_hmac_destroy(struct crypt_hmac *ctx)
291 if (ctx->tfmfd != -1)
295 memset(ctx, 0, sizeof(*ctx));
301 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)