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