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