Handle kernel crypto api init failure better.
[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 -ENOENT;
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 static int crypt_kernel_cipher_available(void)
66 {
67         struct stat st;
68
69         if(stat("/sys/module/algif_skcipher", &st) < 0)
70                 return -ENOENT;
71
72         return -ENOTSUP;
73 }
74
75 /*
76  *ciphers
77  *
78  * ENOENT - no API available
79  * ENOTSUP - algorithm not available
80  */
81 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
82                     const char *mode, const void *buffer, size_t length)
83 {
84         struct crypt_cipher *h;
85         struct sockaddr_alg sa = {
86                 .salg_family = AF_ALG,
87                 .salg_type = "skcipher",
88         };
89         int r;
90
91         h = malloc(sizeof(*h));
92         if (!h)
93                 return -ENOMEM;
94
95         snprintf((char *)sa.salg_name, sizeof(sa.salg_name),
96                  "%s(%s)", mode, name);
97
98         r = crypt_kernel_socket_init(&sa, &h->tfmfd, &h->opfd);
99         if (r < 0) {
100                 free(h);
101                 if (r == -ENOENT)
102                         return crypt_kernel_cipher_available();
103                 return r;
104         }
105
106         if (setsockopt(h->tfmfd, SOL_ALG, ALG_SET_KEY, buffer, length) == -1) {
107                 crypt_cipher_destroy(h);
108                 return -EINVAL;
109         }
110
111         *ctx = h;
112         return 0;
113 }
114
115 /* The in/out should be aligned to page boundary */
116 static int crypt_cipher_crypt(struct crypt_cipher *ctx,
117                          const char *in, char *out, size_t length,
118                          const char *iv, size_t iv_length,
119                          uint32_t direction)
120 {
121         int r = 0;
122         ssize_t len;
123         struct af_alg_iv *alg_iv;
124         struct cmsghdr *header;
125         uint32_t *type;
126         struct iovec iov = {
127                 .iov_base = (void*)(uintptr_t)in,
128                 .iov_len = length,
129         };
130         int iv_msg_size = iv ? CMSG_SPACE(sizeof(*alg_iv) + iv_length) : 0;
131         char buffer[CMSG_SPACE(sizeof(type)) + iv_msg_size];
132         struct msghdr msg = {
133                 .msg_control = buffer,
134                 .msg_controllen = sizeof(buffer),
135                 .msg_iov = &iov,
136                 .msg_iovlen = 1,
137         };
138
139         if (!in || !out || !length)
140                 return -EINVAL;
141
142         if ((!iv && iv_length) || (iv && !iv_length))
143                 return -EINVAL;
144
145         memset(buffer, 0, sizeof(buffer));
146
147         /* Set encrypt/decrypt operation */
148         header = CMSG_FIRSTHDR(&msg);
149         header->cmsg_level = SOL_ALG;
150         header->cmsg_type = ALG_SET_OP;
151         header->cmsg_len = CMSG_LEN(sizeof(type));
152         type = (void*)CMSG_DATA(header);
153         *type = direction;
154
155         /* Set IV */
156         if (iv) {
157                 header = CMSG_NXTHDR(&msg, header);
158                 header->cmsg_level = SOL_ALG;
159                 header->cmsg_type = ALG_SET_IV;
160                 header->cmsg_len = iv_msg_size;
161                 alg_iv = (void*)CMSG_DATA(header);
162                 alg_iv->ivlen = iv_length;
163                 memcpy(alg_iv->iv, iv, iv_length);
164         }
165
166         len = sendmsg(ctx->opfd, &msg, 0);
167         if (len != (ssize_t)length) {
168                 r = -EIO;
169                 goto bad;
170         }
171
172         len = read(ctx->opfd, out, length);
173         if (len != (ssize_t)length)
174                 r = -EIO;
175 bad:
176         memset(buffer, 0, sizeof(buffer));
177         return r;
178 }
179
180 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
181                          const char *in, char *out, size_t length,
182                          const char *iv, size_t iv_length)
183 {
184         return crypt_cipher_crypt(ctx, in, out, length,
185                                   iv, iv_length, ALG_OP_ENCRYPT);
186 }
187
188 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
189                          const char *in, char *out, size_t length,
190                          const char *iv, size_t iv_length)
191 {
192         return crypt_cipher_crypt(ctx, in, out, length,
193                                   iv, iv_length, ALG_OP_DECRYPT);
194 }
195
196 int crypt_cipher_destroy(struct crypt_cipher *ctx)
197 {
198         if (ctx->tfmfd != -1)
199                 close(ctx->tfmfd);
200         if (ctx->opfd != -1)
201                 close(ctx->opfd);
202         memset(ctx, 0, sizeof(*ctx));
203         free(ctx);
204         return 0;
205 }