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