f7d2bcf294bd6a3194274f3acefc539192819b7a
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_cipher_kernel.c
1 /*
2  * Linux kernel userspace API crypto backend implementation (skcipher)
3  *
4  * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2012-2014, Milan Broz
6  *
7  * This file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this file; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include "crypto_backend.h"
30
31 #ifdef ENABLE_AF_ALG
32
33 #include <linux/if_alg.h>
34
35 #ifndef AF_ALG
36 #define AF_ALG 38
37 #endif
38 #ifndef SOL_ALG
39 #define SOL_ALG 279
40 #endif
41
42 struct crypt_cipher {
43         int tfmfd;
44         int opfd;
45 };
46
47 struct cipher_alg {
48         const char *name;
49         int blocksize;
50 };
51
52 /* FIXME: Getting block size should be dynamic from cipher backend. */
53 static struct cipher_alg cipher_algs[] = {
54         { "cipher_null", 16 },
55         { "aes",         16 },
56         { "serpent",     16 },
57         { "twofish",     16 },
58         { "anubis",      16 },
59         { "blowfish",     8 },
60         { "camellia",    16 },
61         { "cast5",        8 },
62         { "cast6",       16 },
63         { "des",          8 },
64         { "des3_ede",     8 },
65         { "khazad",       8 },
66         { "seed",        16 },
67         { "tea",          8 },
68         { "xtea",         8 },
69         { NULL,           0 }
70 };
71
72 static struct cipher_alg *_get_alg(const char *name)
73 {
74         int i = 0;
75
76         while (name && cipher_algs[i].name) {
77                 if (!strcasecmp(name, cipher_algs[i].name))
78                         return &cipher_algs[i];
79                 i++;
80         }
81         return NULL;
82 }
83
84 int crypt_cipher_blocksize(const char *name)
85 {
86         struct cipher_alg *ca = _get_alg(name);
87
88         return ca ? ca->blocksize : -EINVAL;
89 }
90
91 /* Shared with hash kernel backend */
92 int crypt_kernel_socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd);
93
94 int crypt_kernel_socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd)
95 {
96         *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
97         if (*tfmfd == -1)
98                 return -ENOTSUP;
99
100         if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) == -1) {
101                 close(*tfmfd);
102                 *tfmfd = -1;
103                 return -ENOENT;
104         }
105
106         *opfd = accept(*tfmfd, NULL, 0);
107         if (*opfd == -1) {
108                 close(*tfmfd);
109                 *tfmfd = -1;
110                 return -EINVAL;
111         }
112
113         return 0;
114 }
115
116 /*
117  *ciphers
118  *
119  * ENOENT - algorithm not available
120  * ENOTSUP - AF_ALG family not available
121  * (but cannot check specificaly for skcipher API)
122  */
123 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
124                     const char *mode, const void *buffer, size_t length)
125 {
126         struct crypt_cipher *h;
127         struct sockaddr_alg sa = {
128                 .salg_family = AF_ALG,
129                 .salg_type = "skcipher",
130         };
131         int r;
132
133         h = malloc(sizeof(*h));
134         if (!h)
135                 return -ENOMEM;
136
137         snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
138                  "%s(%s)", mode, name);
139
140         r = crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd);
141         if (r < 0) {
142                 free(h);
143                 return r;
144         }
145
146         if (length && strcmp(name, "cipher_null") &&
147             setsockopt(h->tfmfd, SOL_ALG, ALG_SET_KEY, buffer, length) == -1) {
148                 crypt_cipher_destroy(h);
149                 return -EINVAL;
150         }
151
152         *ctx = h;
153         return 0;
154 }
155
156 /* The in/out should be aligned to page boundary */
157 static int crypt_cipher_crypt(struct crypt_cipher *ctx,
158                          const char *in, char *out, size_t length,
159                          const char *iv, size_t iv_length,
160                          uint32_t direction)
161 {
162         int r = 0;
163         ssize_t len;
164         struct af_alg_iv *alg_iv;
165         struct cmsghdr *header;
166         uint32_t *type;
167         struct iovec iov = {
168                 .iov_base = (void*)(uintptr_t)in,
169                 .iov_len = length,
170         };
171         int iv_msg_size = iv ? CMSG_SPACE(sizeof(*alg_iv) + iv_length) : 0;
172         char buffer[CMSG_SPACE(sizeof(*type)) + iv_msg_size];
173         struct msghdr msg = {
174                 .msg_control = buffer,
175                 .msg_controllen = sizeof(buffer),
176                 .msg_iov = &iov,
177                 .msg_iovlen = 1,
178         };
179
180         if (!in || !out || !length)
181                 return -EINVAL;
182
183         if ((!iv && iv_length) || (iv && !iv_length))
184                 return -EINVAL;
185
186         memset(buffer, 0, sizeof(buffer));
187
188         /* Set encrypt/decrypt operation */
189         header = CMSG_FIRSTHDR(&msg);
190         if (!header)
191                 return -EINVAL;
192
193         header->cmsg_level = SOL_ALG;
194         header->cmsg_type = ALG_SET_OP;
195         header->cmsg_len = CMSG_LEN(sizeof(*type));
196         type = (void*)CMSG_DATA(header);
197         *type = direction;
198
199         /* Set IV */
200         if (iv) {
201                 header = CMSG_NXTHDR(&msg, header);
202                 header->cmsg_level = SOL_ALG;
203                 header->cmsg_type = ALG_SET_IV;
204                 header->cmsg_len = iv_msg_size;
205                 alg_iv = (void*)CMSG_DATA(header);
206                 alg_iv->ivlen = iv_length;
207                 memcpy(alg_iv->iv, iv, iv_length);
208         }
209
210         len = sendmsg(ctx->opfd, &msg, 0);
211         if (len != (ssize_t)length) {
212                 r = -EIO;
213                 goto bad;
214         }
215
216         len = read(ctx->opfd, out, length);
217         if (len != (ssize_t)length)
218                 r = -EIO;
219 bad:
220         crypt_backend_memzero(buffer, sizeof(buffer));
221         return r;
222 }
223
224 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
225                          const char *in, char *out, size_t length,
226                          const char *iv, size_t iv_length)
227 {
228         return crypt_cipher_crypt(ctx, in, out, length,
229                                   iv, iv_length, ALG_OP_ENCRYPT);
230 }
231
232 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
233                          const char *in, char *out, size_t length,
234                          const char *iv, size_t iv_length)
235 {
236         return crypt_cipher_crypt(ctx, in, out, length,
237                                   iv, iv_length, ALG_OP_DECRYPT);
238 }
239
240 int crypt_cipher_destroy(struct crypt_cipher *ctx)
241 {
242         if (ctx->tfmfd != -1)
243                 close(ctx->tfmfd);
244         if (ctx->opfd != -1)
245                 close(ctx->opfd);
246         memset(ctx, 0, sizeof(*ctx));
247         free(ctx);
248         return 0;
249 }
250
251 #else /* ENABLE_AF_ALG */
252
253 int crypt_cipher_blocksize(const char *name)
254 {
255         return -EINVAL;
256 }
257
258 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
259                     const char *mode, const void *buffer, size_t length)
260 {
261         return -ENOTSUP;
262 }
263
264 int crypt_cipher_destroy(struct crypt_cipher *ctx)
265 {
266         return 0;
267 }
268
269 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
270                          const char *in, char *out, size_t length,
271                          const char *iv, size_t iv_length)
272 {
273         return -EINVAL;
274 }
275 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
276                          const char *in, char *out, size_t length,
277                          const char *iv, size_t iv_length)
278 {
279         return -EINVAL;
280 }
281 #endif