Add kernel skcipher backend.
[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 <linux/if_alg.h>
28 #include "crypto_backend.h"
29
30 #ifndef AF_ALG
31 #define AF_ALG 38
32 #endif
33 #ifndef SOL_ALG
34 #define SOL_ALG 279
35 #endif
36
37 struct crypt_cipher {
38         int tfmfd;
39         int opfd;
40 };
41
42 int crypt_kernel_socket_init(struct sockaddr_alg *sa, int *tfmfd, int *opfd)
43 {
44         *tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
45         if (*tfmfd == -1)
46                 goto bad;
47
48         if (bind(*tfmfd, (struct sockaddr *)sa, sizeof(*sa)) == -1)
49                 goto bad;
50
51         *opfd = accept(*tfmfd, NULL, 0);
52         if (*opfd == -1)
53                 goto bad;
54
55         return 0;
56 bad:
57         if (*tfmfd != -1) {
58                 close(*tfmfd);
59                 *tfmfd = -1;
60         }
61         if (*opfd != -1) {
62                 close(*opfd);
63                 *opfd = -1;
64         }
65         return -EINVAL;
66 }
67
68 /* ciphers */
69 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
70                     const char *mode, const void *buffer, size_t length)
71 {
72         struct crypt_cipher *h;
73         struct sockaddr_alg sa = {
74                 .salg_family = AF_ALG,
75                 .salg_type = "skcipher",
76         };
77
78         h = malloc(sizeof(*h));
79         if (!h)
80                 return -ENOMEM;
81
82         snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
83                  "%s(%s)", mode, name);
84
85         if (crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd) < 0) {
86                 free(h);
87                 return -ENOTSUP;
88         }
89
90         if (setsockopt(h->tfmfd, SOL_ALG, ALG_SET_KEY, buffer, length) == -1) {
91                 crypt_cipher_destroy(h);
92                 return -EINVAL;
93         }
94
95         *ctx = h;
96         return 0;
97 }
98
99 /* The in/out should be aligned to page boundary */
100 static int crypt_cipher_crypt(struct crypt_cipher *ctx,
101                          const char *in, char *out, size_t length,
102                          const char *iv, size_t iv_length,
103                          uint32_t direction)
104 {
105         int r = 0;
106         ssize_t len;
107         struct af_alg_iv *alg_iv;
108         struct cmsghdr *header;
109         uint32_t *type;
110         struct iovec iov = {
111                 .iov_base = (void*)(uintptr_t)in,
112                 .iov_len = length,
113         };
114         int iv_msg_size = iv ? CMSG_SPACE(sizeof(*alg_iv) + iv_length) : 0;
115         char buffer[CMSG_SPACE(sizeof(type)) + iv_msg_size];
116         struct msghdr msg = {
117                 .msg_control = buffer,
118                 .msg_controllen = sizeof(buffer),
119                 .msg_iov = &iov,
120                 .msg_iovlen = 1,
121         };
122
123         if (!in || !out || !length)
124                 return -EINVAL;
125
126         if ((!iv && iv_length) || (iv && !iv_length))
127                 return -EINVAL;
128
129         memset(buffer, 0, sizeof(buffer));
130
131         /* Set encrypt/decrypt operation */
132         header = CMSG_FIRSTHDR(&msg);
133         header->cmsg_level = SOL_ALG;
134         header->cmsg_type = ALG_SET_OP;
135         header->cmsg_len = CMSG_LEN(sizeof(type));
136         type = (void*)CMSG_DATA(header);
137         *type = direction;
138
139         /* Set IV */
140         if (iv) {
141                 header = CMSG_NXTHDR(&msg, header);
142                 header->cmsg_level = SOL_ALG;
143                 header->cmsg_type = ALG_SET_IV;
144                 header->cmsg_len = iv_msg_size;
145                 alg_iv = (void*)CMSG_DATA(header);
146                 alg_iv->ivlen = iv_length;
147                 memcpy(alg_iv->iv, iv, iv_length);
148         }
149
150         len = sendmsg(ctx->opfd, &msg, 0);
151         if (len != (ssize_t)length) {
152                 r = -EIO;
153                 goto bad;
154         }
155
156         len = read(ctx->opfd, out, length);
157         if (len != (ssize_t)length)
158                 r = -EIO;
159 bad:
160         memset(buffer, 0, sizeof(buffer));
161         return r;
162 }
163
164 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
165                          const char *in, char *out, size_t length,
166                          const char *iv, size_t iv_length)
167 {
168         return crypt_cipher_crypt(ctx, in, out, length,
169                                   iv, iv_length, ALG_OP_ENCRYPT);
170 }
171
172 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
173                          const char *in, char *out, size_t length,
174                          const char *iv, size_t iv_length)
175 {
176         return crypt_cipher_crypt(ctx, in, out, length,
177                                   iv, iv_length, ALG_OP_DECRYPT);
178 }
179
180 int crypt_cipher_destroy(struct crypt_cipher *ctx)
181 {
182         if (ctx->tfmfd != -1)
183                 close(ctx->tfmfd);
184         if (ctx->opfd != -1)
185                 close(ctx->opfd);
186         memset(ctx, 0, sizeof(*ctx));
187         free(ctx);
188         return 0;
189 }