Add skeleton and implementation of various crypto backends
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_kernel.c
1 /*
2  * Linux kernel userspace API crypto backend implementation
3  *
4  * Copyright (C) 2010 Red Hat, Inc. All rights reserved.
5  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <sys/socket.h>
26 #include <sys/utsname.h>
27 #include <linux/if_alg.h>
28 #include "crypto_backend.h"
29
30 /* FIXME: remove later */
31 #ifndef AF_ALG
32 #define AF_ALG 38
33 #define SOL_ALG 279
34 #endif
35
36 struct hash_alg {
37         const char *name;
38         const char *kernel_name;
39         int length;
40 };
41
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 },
48         { NULL, 0 }
49 };
50
51 struct crypt_hash {
52         int tfmfd;
53         int opfd;
54         int hash_len;
55 };
56
57 struct crypt_hmac {
58         int tfmfd;
59         int opfd;
60         int hash_len;
61 };
62
63 static int _socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd)
64 {
65         *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
66         if (*tfmfd == -1)
67                 goto bad;
68
69         if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) == -1)
70                 goto bad;
71
72         *opfd = accept(*tfmfd, NULL, 0);
73         if (*opfd == -1)
74                 goto bad;
75
76         return 0;
77 bad:
78         if (*tfmfd != -1) {
79                 close(*tfmfd);
80                 *tfmfd = -1;
81         }
82         if (*opfd != -1) {
83                 close(*opfd);
84                 *opfd = -1;
85         }
86         return -EINVAL;
87 }
88
89 int crypt_backend_init(void)
90 {
91         struct utsname uts;
92
93         struct sockaddr_alg sa = {
94                 .salg_family = AF_ALG,
95                 .salg_type = "hash",
96                 .salg_name = "sha1",
97         };
98         int tfmfd = -1, opfd = -1;
99
100         log_dbg("Initialising kernel crypto API backend.");
101
102         if (uname(&uts) == -1 || strcmp(uts.sysname, "Linux"))
103                 return -EINVAL;
104         log_dbg("Kernel version %s %s.", uts.sysname, uts.release);
105
106         if (_socket_init(&sa, &tfmfd, &opfd) < 0)
107                 return -EINVAL;
108
109         close(tfmfd);
110         close(opfd);
111         return 0;
112 }
113
114 uint32_t crypt_backend_flags(void)
115 {
116         return CRYPT_BACKEND_KERNEL;
117 }
118
119 static struct hash_alg *_get_alg(const char *name)
120 {
121         int i = 0;
122
123         while (name && hash_algs[i].name) {
124                 if (!strcmp(name, hash_algs[i].name))
125                         return &hash_algs[i];
126                 i++;
127         }
128         return NULL;
129 }
130
131 /* HASH */
132 int crypt_hash_size(const char *name)
133 {
134         struct hash_alg *ha = _get_alg(name);
135
136         return ha ? ha->length : -EINVAL;
137 }
138
139 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
140 {
141         struct crypt_hash *h;
142         struct hash_alg *ha;
143         struct sockaddr_alg sa = {
144                 .salg_family = AF_ALG,
145                 .salg_type = "hash",
146         };
147
148         h = malloc(sizeof(*h));
149         if (!h)
150                 return -ENOMEM;
151
152         ha = _get_alg(name);
153         if (!ha) {
154                 free(h);
155                 return -EINVAL;
156         }
157         h->hash_len = ha->length;
158
159         strncpy((char *)sa.salg_name, ha->kernel_name, sizeof(sa.salg_name));
160
161         if (_socket_init(&sa, &h->tfmfd, &h->opfd) < 0) {
162                 free(h);
163                 return -EINVAL;
164         }
165
166         *ctx = h;
167         return 0;
168 }
169
170 int crypt_hash_restart(struct crypt_hash *ctx)
171 {
172         return 0;
173 }
174
175 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
176 {
177         ssize_t r;
178
179         r = send(ctx->opfd, buffer, length, MSG_MORE);
180         if (r < 0 || r < length)
181                 return -EIO;
182
183         return 0;
184 }
185
186 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
187 {
188         ssize_t r;
189
190         if (length > ctx->hash_len)
191                 return -EINVAL;
192
193         r = read(ctx->opfd, buffer, length);
194         if (r < 0)
195                 return -EIO;
196
197         return 0;
198 }
199
200 int crypt_hash_destroy(struct crypt_hash *ctx)
201 {
202         if (ctx->tfmfd != -1)
203                 close(ctx->tfmfd);
204         if (ctx->opfd != -1)
205                 close(ctx->opfd);
206         memset(ctx, 0, sizeof(*ctx));
207         free(ctx);
208         return 0;
209 }
210
211 /* HMAC */
212 int crypt_hmac_size(const char *name)
213 {
214         return crypt_hash_size(name);
215 }
216
217 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
218                     const void *buffer, size_t length)
219 {
220         struct crypt_hmac *h;
221         struct hash_alg *ha;
222         struct sockaddr_alg sa = {
223                 .salg_family = AF_ALG,
224                 .salg_type = "hash",
225         };
226
227         h = malloc(sizeof(*h));
228         if (!h)
229                 return -ENOMEM;
230
231         ha = _get_alg(name);
232         if (!ha) {
233                 free(h);
234                 return -EINVAL;
235         }
236         h->hash_len = ha->length;
237
238         snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
239                  "hmac(%s)", ha->kernel_name);
240
241         if (_socket_init(&sa, &h->tfmfd, &h->opfd) < 0) {
242                 free(h);
243                 return -EINVAL;
244         }
245
246         if (setsockopt(h->tfmfd, SOL_ALG, ALG_SET_KEY, buffer, length) == -1) {
247                 crypt_hmac_destroy(h);
248                 return -EINVAL;
249         }
250
251         *ctx = h;
252         return 0;
253 }
254
255 int crypt_hmac_restart(struct crypt_hmac *ctx)
256 {
257         return 0;
258 }
259
260 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
261 {
262         ssize_t r;
263
264         r = send(ctx->opfd, buffer, length, MSG_MORE);
265         if (r < 0 || r < length)
266                 return -EIO;
267
268         return 0;
269 }
270
271 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
272 {
273         ssize_t r;
274
275         if (length > ctx->hash_len)
276                 return -EINVAL;
277
278         r = read(ctx->opfd, buffer, length);
279         if (r < 0)
280                 return -EIO;
281
282         return 0;
283 }
284
285 int crypt_hmac_destroy(struct crypt_hmac *ctx)
286 {
287         if (ctx->tfmfd != -1)
288                 close(ctx->tfmfd);
289         if (ctx->opfd != -1)
290                 close(ctx->opfd);
291         memset(ctx, 0, sizeof(*ctx));
292         free(ctx);
293         return 0;
294 }