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