41cdf18c40568960c4d44f55e99114a911b16319
[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, Milan Broz
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 #include <linux/if_alg.h>
29 #include "crypto_backend.h"
30
31 #ifndef AF_ALG
32 #define AF_ALG 38
33 #endif
34 #ifndef SOL_ALG
35 #define SOL_ALG 279
36 #endif
37
38 struct crypt_cipher {
39         int tfmfd;
40         int opfd;
41 };
42
43 int crypt_kernel_socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd)
44 {
45         *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
46         if (*tfmfd == -1)
47                 return -ENOTSUP;
48
49         if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) == -1) {
50                 close(*tfmfd);
51                 *tfmfd = -1;
52                 return -ENOENT;
53         }
54
55         *opfd = accept(*tfmfd, NULL, 0);
56         if (*opfd == -1) {
57                 close(*tfmfd);
58                 *tfmfd = -1;
59                 return -EINVAL;
60         }
61
62         return 0;
63 }
64
65 /*
66  *ciphers
67  *
68  * ENOENT - algorithm not available
69  * ENOTSUP - AF_ALG family not available
70  * (but cannot check specificaly for skcipher API)
71  */
72 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
73                     const char *mode, const void *buffer, size_t length)
74 {
75         struct crypt_cipher *h;
76         struct sockaddr_alg sa = {
77                 .salg_family = AF_ALG,
78                 .salg_type = "skcipher",
79         };
80         int r;
81
82         h = malloc(sizeof(*h));
83         if (!h)
84                 return -ENOMEM;
85
86         snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
87                  "%s(%s)", mode, name);
88
89         r = crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd);
90         if (r < 0) {
91                 free(h);
92                 return r;
93         }
94
95         if (setsockopt(h->tfmfd, SOL_ALG, ALG_SET_KEY, buffer, length) == -1) {
96                 crypt_cipher_destroy(h);
97                 return -EINVAL;
98         }
99
100         *ctx = h;
101         return 0;
102 }
103
104 /* The in/out should be aligned to page boundary */
105 static int crypt_cipher_crypt(struct crypt_cipher *ctx,
106                          const char *in, char *out, size_t length,
107                          const char *iv, size_t iv_length,
108                          uint32_t direction)
109 {
110         int r = 0;
111         ssize_t len;
112         struct af_alg_iv *alg_iv;
113         struct cmsghdr *header;
114         uint32_t *type;
115         struct iovec iov = {
116                 .iov_base = (void*)(uintptr_t)in,
117                 .iov_len = length,
118         };
119         int iv_msg_size = iv ? CMSG_SPACE(sizeof(*alg_iv) + iv_length) : 0;
120         char buffer[CMSG_SPACE(sizeof(type)) + iv_msg_size];
121         struct msghdr msg = {
122                 .msg_control = buffer,
123                 .msg_controllen = sizeof(buffer),
124                 .msg_iov = &iov,
125                 .msg_iovlen = 1,
126         };
127
128         if (!in || !out || !length)
129                 return -EINVAL;
130
131         if ((!iv && iv_length) || (iv && !iv_length))
132                 return -EINVAL;
133
134         memset(buffer, 0, sizeof(buffer));
135
136         /* Set encrypt/decrypt operation */
137         header = CMSG_FIRSTHDR(&msg);
138         header->cmsg_level = SOL_ALG;
139         header->cmsg_type = ALG_SET_OP;
140         header->cmsg_len = CMSG_LEN(sizeof(type));
141         type = (void*)CMSG_DATA(header);
142         *type = direction;
143
144         /* Set IV */
145         if (iv) {
146                 header = CMSG_NXTHDR(&msg, header);
147                 header->cmsg_level = SOL_ALG;
148                 header->cmsg_type = ALG_SET_IV;
149                 header->cmsg_len = iv_msg_size;
150                 alg_iv = (void*)CMSG_DATA(header);
151                 alg_iv->ivlen = iv_length;
152                 memcpy(alg_iv->iv, iv, iv_length);
153         }
154
155         len = sendmsg(ctx->opfd, &msg, 0);
156         if (len != (ssize_t)length) {
157                 r = -EIO;
158                 goto bad;
159         }
160
161         len = read(ctx->opfd, out, length);
162         if (len != (ssize_t)length)
163                 r = -EIO;
164 bad:
165         memset(buffer, 0, sizeof(buffer));
166         return r;
167 }
168
169 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
170                          const char *in, char *out, size_t length,
171                          const char *iv, size_t iv_length)
172 {
173         return crypt_cipher_crypt(ctx, in, out, length,
174                                   iv, iv_length, ALG_OP_ENCRYPT);
175 }
176
177 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
178                          const char *in, char *out, size_t length,
179                          const char *iv, size_t iv_length)
180 {
181         return crypt_cipher_crypt(ctx, in, out, length,
182                                   iv, iv_length, ALG_OP_DECRYPT);
183 }
184
185 int crypt_cipher_destroy(struct crypt_cipher *ctx)
186 {
187         if (ctx->tfmfd != -1)
188                 close(ctx->tfmfd);
189         if (ctx->opfd != -1)
190                 close(ctx->opfd);
191         memset(ctx, 0, sizeof(*ctx));
192         free(ctx);
193         return 0;
194 }