2 * Linux kernel userspace API crypto backend implementation
4 * Copyright (C) 2010 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 const char *kernel_name;
42 static struct hash_alg hash_algs[] = {
43 { "sha1", "sha1", 20 },
44 { "sha256", "sha256", 32 },
45 { "sha512", "sha512", 64 },
46 { "ripemd160", "rmd160", 20 },
47 { "whirlpool", "wp512", 64 },
63 static int _socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd)
65 *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
69 if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) == -1)
72 *opfd = accept(*tfmfd, NULL, 0);
89 int crypt_backend_init(void)
93 struct sockaddr_alg sa = {
94 .salg_family = AF_ALG,
98 int tfmfd = -1, opfd = -1;
100 log_dbg("Initialising kernel crypto API backend.");
102 if (uname(&uts) == -1 || strcmp(uts.sysname, "Linux"))
104 log_dbg("Kernel version %s %s.", uts.sysname, uts.release);
106 if (_socket_init(&sa, &tfmfd, &opfd) < 0)
114 uint32_t crypt_backend_flags(void)
116 return CRYPT_BACKEND_KERNEL;
119 static struct hash_alg *_get_alg(const char *name)
123 while (name && hash_algs[i].name) {
124 if (!strcmp(name, hash_algs[i].name))
125 return &hash_algs[i];
132 int crypt_hash_size(const char *name)
134 struct hash_alg *ha = _get_alg(name);
136 return ha ? ha->length : -EINVAL;
139 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
141 struct crypt_hash *h;
143 struct sockaddr_alg sa = {
144 .salg_family = AF_ALG,
148 h = malloc(sizeof(*h));
157 h->hash_len = ha->length;
159 strncpy((char *)sa.salg_name, ha->kernel_name, sizeof(sa.salg_name));
161 if (_socket_init(&sa, &h->tfmfd, &h->opfd) < 0) {
170 int crypt_hash_restart(struct crypt_hash *ctx)
175 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
179 r = send(ctx->opfd, buffer, length, MSG_MORE);
180 if (r < 0 || r < length)
186 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
190 if (length > ctx->hash_len)
193 r = read(ctx->opfd, buffer, length);
200 int crypt_hash_destroy(struct crypt_hash *ctx)
202 if (ctx->tfmfd != -1)
206 memset(ctx, 0, sizeof(*ctx));
212 int crypt_hmac_size(const char *name)
214 return crypt_hash_size(name);
217 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
218 const void *buffer, size_t length)
220 struct crypt_hmac *h;
222 struct sockaddr_alg sa = {
223 .salg_family = AF_ALG,
227 h = malloc(sizeof(*h));
236 h->hash_len = ha->length;
238 snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
239 "hmac(%s)", ha->kernel_name);
241 if (_socket_init(&sa, &h->tfmfd, &h->opfd) < 0) {
246 if (setsockopt(h->tfmfd, SOL_ALG, ALG_SET_KEY, buffer, length) == -1) {
247 crypt_hmac_destroy(h);
255 int crypt_hmac_restart(struct crypt_hmac *ctx)
260 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
264 r = send(ctx->opfd, buffer, length, MSG_MORE);
265 if (r < 0 || r < length)
271 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
275 if (length > ctx->hash_len)
278 r = read(ctx->opfd, buffer, length);
285 int crypt_hmac_destroy(struct crypt_hmac *ctx)
287 if (ctx->tfmfd != -1)
291 memset(ctx, 0, sizeof(*ctx));