Debian Codegraph @

contrib/linux-crypto.c

raw symbols SCIP
1 /* Copyright © 2014-2019, Stephan Mueller <[email protected]>
2 * Copyright © 2020 in2ip B.V.
3 * Linux Crypto API, based on libkcapi, relicensed to BSD-2 with author's permission
4 * Author: Stephan Mueller <[email protected]>
5 * Modified for librist by: Gijs Peskens <[email protected]>
6 * Modified for librist by: Sergio Ammirata <[email protected]>
7 *
8 * SPDX-License-Identifier: BSD-2-Clause
9 */
10
11 #ifdef __linux__
12 #include "linux-crypto.h"
13
14 struct linux_crypto {
15 int sockfd;
16 int cryptofd;
17 struct kcapi_handle *handle;
18 };
19
20
21 int32_t _linux_crypto_send_crypt(struct linux_crypto *ctx,
22 struct iovec *iov, uint32_t iovlen,
23 uint32_t enc, const uint8_t *iv)
24 {
25 int32_t ret;
26 char buffer_static[80] = { 0 };
27 char *buffer_p = buffer_static;
28 char *buffer_alloc = NULL;
29
30 /* plaintext / ciphertext data */
31 struct cmsghdr *header = NULL;
32 uint32_t *type = NULL;
33 struct msghdr msg;
34
35 /* IV data */
36 struct af_alg_iv *alg_iv = NULL;
37 uint32_t iv_msg_size = CMSG_SPACE(sizeof(*alg_iv) + 16);
38
39
40 uint32_t bufferlen = CMSG_SPACE(sizeof(*type)) + /* Encryption / Decryption */
41 iv_msg_size; /* IV */
42
43 memset(&msg, 0, sizeof(msg));
44
45 /* allocate buffer, if static buffer is too small */
46 if (RIST_UNLIKELY(bufferlen > sizeof(buffer_static))) {
47 buffer_alloc = calloc(1, bufferlen);
48 if (!buffer_alloc)
49 return -ENOMEM;
50 buffer_p = buffer_alloc;
51 }
52
53 msg.msg_control = buffer_p;
54 msg.msg_controllen = bufferlen;
55 msg.msg_iov = iov;
56 msg.msg_iovlen = iovlen;
57
58 /* encrypt/decrypt operation */
59 header = CMSG_FIRSTHDR(&msg);
60 if (RIST_UNLIKELY(!header)) {
61 ret = -EFAULT;
62 goto out;
63 }
64 header->cmsg_level = SOL_ALG;
65 header->cmsg_type = ALG_SET_OP;
66 header->cmsg_len = CMSG_LEN(sizeof(*type));
67 type = (void*)CMSG_DATA(header);
68 *type = enc;
69
70 /* set IV */
71 header = CMSG_NXTHDR(&msg, header);
72 if (RIST_UNLIKELY(!header)) {
73 ret = -EFAULT;
74 goto out;
75 }
76 header->cmsg_level = SOL_ALG;
77 header->cmsg_type = ALG_SET_IV;
78 header->cmsg_len = iv_msg_size;
79 alg_iv = (void*)CMSG_DATA(header);
80 alg_iv->ivlen = 16;
81 memcpy(alg_iv->iv, iv, 16);
82
83
84 ret = (int32_t)sendmsg(ctx->cryptofd, &msg, 0);
85 if (RIST_UNLIKELY(ret < 0))
86 ret = -errno;
87
88 out:
89 if (buffer_alloc)
90 free(buffer_alloc);
91 return ret;
92 }
93
94 int32_t _linux_crypto_read(struct linux_crypto *ctx,
95 uint8_t *out, uint32_t outlen)
96 {
97 int ret;
98 int32_t totallen = 0;
99
100 if (RIST_LIKELY(outlen)) {
101 do {
102 ret = (int32_t)read(ctx->cryptofd, out, outlen);
103 if (ret > 0) {
104 out += ret;
105 outlen -= ret;
106 totallen += ret;
107 }
108 } while ((ret > 0 || errno == EINTR) && outlen);
109
110 if (RIST_UNLIKELY(ret < 0))
111 return -errno;
112 }
113
114 return totallen;
115 }
116
117 int32_t _linux_crypto_process(struct linux_crypto *ctx,
118 const uint8_t *in, uint32_t inlen,
119 uint8_t *out, uint32_t outlen,
120 int access,const uint8_t *iv, int enc)
121 {
122 RIST_MARK_UNUSED(access);
123 int32_t totallen = 0;
124 int32_t ret;
125
126 struct iovec iov;
127
128 if (RIST_UNLIKELY(!ctx->cryptofd)) {
129 return -1;
130 }
131
132 while (inlen && outlen) {
133 uint32_t inprocess = inlen;
134 uint32_t outprocess = outlen;
135
136 iov.iov_base = (void*)(uintptr_t)in;
137 iov.iov_len = inprocess;
138 ret = _linux_crypto_send_crypt(ctx, &iov, 1, enc, iv);
139 if (RIST_UNLIKELY(0 > ret)) {
140 return ret;
141 }
142 ret = _linux_crypto_read(ctx, out, outprocess);
143
144 if (RIST_UNLIKELY(ret < 0))
145 return ret;
146
147 totallen += inprocess;
148 in += inprocess;
149 inlen -= inprocess;
150 out += ret;
151 outlen -= ret;
152 }
153
154 return totallen;
155 }
156
157 int linux_crypto_init(struct linux_crypto **_ctx) {
158
159 struct linux_crypto *ctx = calloc(1, sizeof(*ctx));
160 int ret;
161 struct sockaddr_alg sa = {
162 .salg_family = AF_ALG,
163 .salg_type = "skcipher", /* this selects the symmetric cipher */
164 .salg_name = "ctr(aes)" /* this is the cipher name */
165 };
166 ctx->sockfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
167 if (ctx->sockfd == -1) {
168 fprintf(stderr, "Failed to set up socket!\n");
169 free(ctx);
170 return -1;
171 }
172 ret = bind(ctx->sockfd, (struct sockaddr *)&sa, sizeof(sa));
173 if (ret == -1) {
174 fprintf(stderr, "Failed to bind to socket!\n");
175 free(ctx);
176 return ret;
177 }
178
179 *_ctx = ctx;
180 return 0;
181
182 }
183
184 void linux_crypto_free(struct linux_crypto **ctx) {
185 if (!ctx || !*ctx) {
186 return;
187 }
188
189 if ((*ctx)->cryptofd)
190 close((*ctx)->cryptofd);
191 if ((*ctx)->sockfd)
192 close((*ctx)->sockfd);
193 free(*ctx);
194 *ctx = NULL;
195 }
196
197 int linux_crypto_set_key(const uint8_t *key, int keylen, struct linux_crypto *ctx) {
198 int ret;
199
200 if (ctx->cryptofd) {
201 close(ctx->cryptofd);
202 ctx->cryptofd = 0;
203 }
204
205 ret = setsockopt(ctx->sockfd, SOL_ALG, ALG_SET_KEY, key, keylen);
206 if (ret < 0) {
207 fprintf(stderr, "Errno is %d\n", -errno);
208 fprintf(stderr, "Failed to set key!\n");
209 return -1;
210 }
211
212 ret = accept(ctx->sockfd, NULL, 0);
213 if (ret == -1) {
214 fprintf(stderr, "Failed to accept socket!\n");
215 return ret;
216 }
217 ctx->cryptofd = ret;
218 return 0;
219 }
220
221 int linux_crypto_decrypt(const uint8_t inbuf[], uint8_t outbuf[], int buflen, uint8_t iv[], struct linux_crypto *ctx) {
222 return _linux_crypto_process(ctx, inbuf, buflen, outbuf, buflen, 0, iv, 0);
223 }
224 int linux_crypto_encrypt(const uint8_t inbuf[], uint8_t outbuf[], int buflen, uint8_t iv[], struct linux_crypto *ctx) {
225 return _linux_crypto_process(ctx, inbuf, buflen, outbuf, buflen, 1, iv, 0);
226 }
227
228 #endif