2 * Linux kernel userspace API crypto backend implementation
4 * Copyright (C) 2010-2011, 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 static int crypto_backend_initialised = 0;
42 const char *kernel_name;
46 static struct hash_alg hash_algs[] = {
47 { "sha1", "sha1", 20 },
48 { "sha256", "sha256", 32 },
49 { "sha512", "sha512", 64 },
50 { "ripemd160", "rmd160", 20 },
51 { "whirlpool", "wp512", 64 },
67 static int _socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd)
69 *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
73 if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) == -1)
76 *opfd = accept(*tfmfd, NULL, 0);
93 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 log_dbg("Initialising kernel crypto API backend.");
109 if (uname(&uts) == -1 || strcmp(uts.sysname, "Linux"))
111 log_dbg("Kernel version %s %s.", uts.sysname, uts.release);
113 if (_socket_init(&sa, &tfmfd, &opfd) < 0)
119 crypto_backend_initialised = 1;
123 uint32_t crypt_backend_flags(void)
125 return CRYPT_BACKEND_KERNEL;
128 static struct hash_alg *_get_alg(const char *name)
132 while (name && hash_algs[i].name) {
133 if (!strcmp(name, hash_algs[i].name))
134 return &hash_algs[i];
141 int crypt_hash_size(const char *name)
143 struct hash_alg *ha = _get_alg(name);
145 return ha ? ha->length : -EINVAL;
148 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
150 struct crypt_hash *h;
152 struct sockaddr_alg sa = {
153 .salg_family = AF_ALG,
157 h = malloc(sizeof(*h));
166 h->hash_len = ha->length;
168 strncpy((char *)sa.salg_name, ha->kernel_name, sizeof(sa.salg_name));
170 if (_socket_init(&sa, &h->tfmfd, &h->opfd) < 0) {
179 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
183 r = send(ctx->opfd, buffer, length, MSG_MORE);
184 if (r < 0 || (size_t)r < length)
190 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
194 if (length > (size_t)ctx->hash_len)
197 r = read(ctx->opfd, buffer, length);
204 int crypt_hash_destroy(struct crypt_hash *ctx)
206 if (ctx->tfmfd != -1)
210 memset(ctx, 0, sizeof(*ctx));
216 int crypt_hmac_size(const char *name)
218 return crypt_hash_size(name);
221 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
222 const void *buffer, size_t length)
224 struct crypt_hmac *h;
226 struct sockaddr_alg sa = {
227 .salg_family = AF_ALG,
231 h = malloc(sizeof(*h));
240 h->hash_len = ha->length;
242 snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
243 "hmac(%s)", ha->kernel_name);
245 if (_socket_init(&sa, &h->tfmfd, &h->opfd) < 0) {
250 if (setsockopt(h->tfmfd, SOL_ALG, ALG_SET_KEY, buffer, length) == -1) {
251 crypt_hmac_destroy(h);
259 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
263 r = send(ctx->opfd, buffer, length, MSG_MORE);
264 if (r < 0 || (size_t)r < length)
270 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
274 if (length > (size_t)ctx->hash_len)
277 r = read(ctx->opfd, buffer, length);
284 int crypt_hmac_destroy(struct crypt_hmac *ctx)
286 if (ctx->tfmfd != -1)
290 memset(ctx, 0, sizeof(*ctx));