File: linux-crypto.c

package info (click to toggle)
librist 0.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,100 kB
  • sloc: ansic: 15,906; sh: 81; makefile: 6
file content (228 lines) | stat: -rw-r--r-- 5,191 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Copyright © 2014-2019, Stephan Mueller <smueller@chronox.de>
 * Copyright © 2020 in2ip B.V.
 * Linux Crypto API, based on libkcapi, relicensed to BSD-2 with author's permission
 * Author: Stephan Mueller <smueller@chronox.de>
 * Modified for librist by: Gijs Peskens <gijs@in2ip.nl>
 * Modified for librist by: Sergio Ammirata <sergio@ammirata.net>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#ifdef __linux
#include "linux-crypto.h"

struct linux_crypto {
	int sockfd;
	int cryptofd;
	struct kcapi_handle *handle;
};


int32_t _linux_crypto_send_crypt(struct linux_crypto *ctx,
				struct iovec *iov, uint32_t iovlen,
				uint32_t enc, const uint8_t *iv)
{
	int32_t ret;
	char buffer_static[80] = { 0 };
	char *buffer_p = buffer_static;
	char *buffer_alloc = NULL;

	/* plaintext / ciphertext data */
	struct cmsghdr *header = NULL;
	uint32_t *type = NULL;
	struct msghdr msg;

	/* IV data */
	struct af_alg_iv *alg_iv = NULL;
	uint32_t iv_msg_size = CMSG_SPACE(sizeof(*alg_iv) + 16);


	uint32_t bufferlen = CMSG_SPACE(sizeof(*type)) + 	/* Encryption / Decryption */
		iv_msg_size; 			/* IV */

	memset(&msg, 0, sizeof(msg));

	/* allocate buffer, if static buffer is too small */
	if (RIST_UNLIKELY(bufferlen > sizeof(buffer_static))) {
		buffer_alloc = calloc(1, bufferlen);
		if (!buffer_alloc)
			return -ENOMEM;
		buffer_p = buffer_alloc;
	}

	msg.msg_control = buffer_p;
	msg.msg_controllen = bufferlen;
	msg.msg_iov = iov;
	msg.msg_iovlen = iovlen;

	/* encrypt/decrypt operation */
	header = CMSG_FIRSTHDR(&msg);
	if (RIST_UNLIKELY(!header)) {
		ret = -EFAULT;
		goto out;
	}
	header->cmsg_level = SOL_ALG;
	header->cmsg_type = ALG_SET_OP;
	header->cmsg_len = CMSG_LEN(sizeof(*type));
	type = (void*)CMSG_DATA(header);
	*type = enc;

	/* set IV */
	header = CMSG_NXTHDR(&msg, header);
	if (RIST_UNLIKELY(!header)) {
		ret = -EFAULT;
		goto out;
	}
	header->cmsg_level = SOL_ALG;
	header->cmsg_type = ALG_SET_IV;
	header->cmsg_len = iv_msg_size;
	alg_iv = (void*)CMSG_DATA(header);
	alg_iv->ivlen = 16;
	memcpy(alg_iv->iv, iv, 16);


	ret = (int32_t)sendmsg(ctx->cryptofd, &msg, 0);
	if (RIST_UNLIKELY(ret < 0))
		ret = -errno;

out:
	if (buffer_alloc)
		free(buffer_alloc);
	return ret;
}

int32_t _linux_crypto_read(struct linux_crypto *ctx,
				   uint8_t *out, uint32_t outlen)
{
	int ret;
	int32_t totallen = 0;

	if (RIST_LIKELY(outlen)) {
		do {
			ret = (int32_t)read(ctx->cryptofd, out, outlen);
			if (ret > 0) {
				out += ret;
				outlen -= ret;
				totallen += ret;
			}
		} while ((ret > 0 || errno == EINTR) && outlen);

		if (RIST_UNLIKELY(ret < 0))
			return -errno;
	}

	return totallen;
}

int32_t _linux_crypto_process(struct linux_crypto *ctx,
				  const uint8_t *in, uint32_t inlen,
				  uint8_t *out, uint32_t outlen,
				  int access,const uint8_t *iv, int enc)
{
	RIST_MARK_UNUSED(access);
	int32_t totallen = 0;
	int32_t ret;

	struct iovec iov;

	if (RIST_UNLIKELY(!ctx->cryptofd)) {
		return -1;
	}

	while (inlen && outlen) {
		uint32_t inprocess = inlen;
		uint32_t outprocess = outlen;

		iov.iov_base = (void*)(uintptr_t)in;
		iov.iov_len = inprocess;
		ret = _linux_crypto_send_crypt(ctx, &iov, 1, enc, iv);
		if (RIST_UNLIKELY(0 > ret)) {
			return ret;
		}
		ret = _linux_crypto_read(ctx, out, outprocess);

		if (RIST_UNLIKELY(ret < 0))
			return ret;

		totallen += inprocess;
		in += inprocess;
		inlen -= inprocess;
		out += ret;
		outlen -= ret;
	}

	return totallen;
}

int linux_crypto_init(struct linux_crypto **_ctx) {

	struct linux_crypto *ctx = calloc(1, sizeof(*ctx));
	int ret;
	struct sockaddr_alg sa = {
		.salg_family = AF_ALG,
		.salg_type = "skcipher", /* this selects the symmetric cipher */
		.salg_name = "ctr(aes)" /* this is the cipher name */
	};
	ctx->sockfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
	if (ctx->sockfd == -1) {
		fprintf(stderr, "Failed to set up socket!\n");
		free(ctx);
		return -1;
	}
	ret = bind(ctx->sockfd, (struct sockaddr *)&sa, sizeof(sa));
	if (ret == -1) {
		fprintf(stderr, "Failed to bind to socket!\n");
		free(ctx);
		return ret;
	}

	*_ctx = ctx;
	return 0;

}

void linux_crypto_free(struct linux_crypto **ctx) {
	if (!ctx || !*ctx) {
		return;
	}

	if ((*ctx)->cryptofd)
		close((*ctx)->cryptofd);
	if ((*ctx)->sockfd)
		close((*ctx)->sockfd);
	free(*ctx);
	*ctx = NULL;
}

int linux_crypto_set_key(const uint8_t *key, int keylen, struct linux_crypto *ctx) {
	int ret;

	if (ctx->cryptofd) {
		close(ctx->cryptofd);
		ctx->cryptofd = 0;
	}

	ret = setsockopt(ctx->sockfd, SOL_ALG, ALG_SET_KEY, key, keylen);
	if (ret < 0) {
		fprintf(stderr, "Errno is %d\n", -errno);
		fprintf(stderr, "Failed to set key!\n");
		return -1;
	}

	ret = accept(ctx->sockfd, NULL, 0);
	if (ret == -1) {
		fprintf(stderr, "Failed to accept socket!\n");
		return ret;
	}
	ctx->cryptofd = ret;
	return 0;
}

int linux_crypto_decrypt(uint8_t inbuf[], uint8_t outbuf[], int buflen, uint8_t iv[], struct linux_crypto *ctx) {
	return _linux_crypto_process(ctx, inbuf, buflen, outbuf, buflen, 0, iv, 0);
}
int linux_crypto_encrypt(uint8_t inbuf[], uint8_t outbuf[], int buflen, uint8_t iv[], struct linux_crypto *ctx) {
	return _linux_crypto_process(ctx, inbuf, buflen, outbuf, buflen, 1, iv, 0);
}

#endif