summaryrefslogtreecommitdiffstats
path: root/src/crypto_linux.c
blob: e3f3dd975777690d2811e3da83e38ca1e85f944c (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
  Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "fastd.h"
#include "crypto.h"

#include <alloca.h>
#include <linux/if_alg.h>


#ifndef SOL_ALG
#define SOL_ALG 279
#endif


#ifdef USE_CRYPTO_AES128CTR
#ifdef WITH_CRYPTO_AES128CTR_LINUX

struct _fastd_crypto_aes128ctr_context {
	int fd;
};

struct _fastd_crypto_aes128ctr_state {
	int fd;
};

static fastd_crypto_aes128ctr_context* aes128ctr_init(fastd_context *ctx) {
	int fd = socket(AF_ALG, SOCK_SEQPACKET, 0);
	if (fd < 0)
		goto error;

	struct sockaddr_alg sa = {};
	sa.salg_family = AF_ALG;
	strcpy((char*)sa.salg_type, "skcipher");
	strcpy((char*)sa.salg_name, "ctr(aes)");
	if (bind(fd, (struct sockaddr*)&sa, sizeof(sa)) < 0)
		goto error;

	fastd_crypto_aes128ctr_context *cctx = malloc(sizeof(fastd_crypto_aes128ctr_context));
	cctx->fd = fd;
	return cctx;

 error:
	if (fd >= 0)
		close(fd);

	pr_error(ctx, "no kernel support for AES-CTR was found");
	return NULL;
}

static fastd_crypto_aes128ctr_state* aes128ctr_set_key(fastd_context *ctx, const fastd_crypto_aes128ctr_context *cctx, const fastd_block128 *key) {
	if (setsockopt(cctx->fd, SOL_ALG, ALG_SET_KEY, key->b, 16) < 0) {
		pr_error_errno(ctx, "aes128ctr_set_key(linux): setsockopt");
		return NULL;
	}

	int fd = accept(cctx->fd, NULL, NULL);

	if (fd < 0) {
		pr_error_errno(ctx, "aes128ctr_set_key(linux): accept");
		return NULL;
	}

	fastd_crypto_aes128ctr_state *cstate = malloc(sizeof(fastd_crypto_aes128ctr_state));
	cstate->fd = fd;

	return cstate;
}

static bool aes128ctr_crypt(fastd_context *ctx, const fastd_crypto_aes128ctr_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t len, const fastd_block128 *iv) {
	if (!len)
		return false;

	struct iovec vec = { .iov_base = (void*)in, .iov_len = len };

	static const size_t cmsglen = sizeof(struct cmsghdr)+sizeof(struct af_alg_iv)+16;
	struct cmsghdr *cmsg = alloca(cmsglen);
	cmsg->cmsg_len = cmsglen;
	cmsg->cmsg_level = SOL_ALG;
	cmsg->cmsg_type = ALG_SET_IV;

	struct af_alg_iv *alg_iv = (void*)CMSG_DATA(cmsg);
	alg_iv->ivlen = 16;
	memcpy(alg_iv->iv, iv, 16);

	struct msghdr msg = {
		.msg_iov = &vec,
		.msg_iovlen = 1,
		.msg_control = cmsg,
		.msg_controllen = cmsglen
	};

	if (sendmsg(cstate->fd, &msg, 0) < 0) {
		pr_error_errno(ctx, "aes128ctr_crypt(linux): sendmsg");
		return false;
	}

	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	vec.iov_base = out;

	if (recvmsg(cstate->fd, &msg, 0) < 0) {
		pr_error_errno(ctx, "aes128ctr_crypt(linux): recvmsg");
		return false;
	}

	return true;
}

static void aes128ctr_free_state(fastd_context *ctx, fastd_crypto_aes128ctr_state *cstate) {
	if (cstate) {
		close(cstate->fd);
		free(cstate);
	}
}

static void aes128ctr_free(fastd_context *ctx, fastd_crypto_aes128ctr_context *cctx) {
	if (cctx) {
		close(cctx->fd);
		free(cctx);
	}
}

fastd_crypto_aes128ctr fastd_crypto_aes128ctr_linux = {
	.name = "linux",

	.init = aes128ctr_init,
	.set_key = aes128ctr_set_key,
	.crypt = aes128ctr_crypt,

	.free_state = aes128ctr_free_state,
	.free = aes128ctr_free,
};

#endif
#endif

#ifdef USE_CRYPTO_GHASH
#ifdef WITH_CRYPTO_GHASH_LINUX

struct _fastd_crypto_ghash_context {
	int fd;
};

struct _fastd_crypto_ghash_state {
	int fd;
};

static fastd_crypto_ghash_context* ghash_init(fastd_context *ctx) {
	int fd = socket(AF_ALG, SOCK_SEQPACKET, 0);
	if (fd < 0)
		goto error;

	struct sockaddr_alg sa = {};
	sa.salg_family = AF_ALG;
	strcpy((char*)sa.salg_type, "hash");
	strcpy((char*)sa.salg_name, "ghash");
	if (bind(fd, (struct sockaddr*)&sa, sizeof(sa)) < 0)
		goto error;

	fastd_crypto_ghash_context *cctx = malloc(sizeof(fastd_crypto_ghash_context));
	cctx->fd = fd;
	return cctx;

 error:
	if (fd >= 0)
		close(fd);

	pr_error(ctx, "no kernel support for GHASH was found");
	return NULL;
}

static fastd_crypto_ghash_state* ghash_set_h(fastd_context *ctx, const fastd_crypto_ghash_context *cctx, const fastd_block128 *h) {
	if (setsockopt(cctx->fd, SOL_ALG, ALG_SET_KEY, h, 16) < 0) {
		pr_error_errno(ctx, "ghash_set_h(linux): setsockopt");
		return NULL;
	}

	int fd = accept(cctx->fd, NULL, NULL);

	if (fd < 0) {
		pr_error_errno(ctx, "ghash_set_h(linux): accept");
		return NULL;
	}

	fastd_crypto_ghash_state *cstate = malloc(sizeof(fastd_crypto_ghash_state));
	cstate->fd = fd;

	return cstate;
}

static bool ghash_hash(fastd_context *ctx, const fastd_crypto_ghash_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t n_blocks) {
	if (!n_blocks)
		return false;

	if (write(cstate->fd, in, n_blocks*16) < 0) {
		pr_error_errno(ctx, "ghash_hash(linux): write");
		return false;
	}

	if (read(cstate->fd, out, 16) < 16) {
		pr_error_errno(ctx, "ghash_hash(linux): read");
		return false;
	}

	return true;
}

static void ghash_free_state(fastd_context *ctx, fastd_crypto_ghash_state *cstate) {
	if (cstate) {
		close(cstate->fd);
		free(cstate);
	}
}

static void ghash_free(fastd_context *ctx, fastd_crypto_ghash_context *cctx) {
	if (cctx) {
		close(cctx->fd);
		free(cctx);
	}
}

fastd_crypto_ghash fastd_crypto_ghash_linux = {
	.name = "linux",

	.init = ghash_init,
	.set_h = ghash_set_h,
	.hash = ghash_hash,

	.free_state = ghash_free_state,
	.free = ghash_free,
};

#endif
#endif