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
|
/*
* Copyright (C) 2010 Martin Willi
* Copyright (C) 2010 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "af_alg_ops.h"
#include <unistd.h>
#include <errno.h>
#include <linux/socket.h>
#include <utils/debug.h>
typedef struct private_af_alg_ops_t private_af_alg_ops_t;
/**
* Private data of an af_alg_ops_t object.
*/
struct private_af_alg_ops_t {
/**
* Public af_alg_ops_t interface.
*/
af_alg_ops_t public;
/**
* Transform FD
*/
int tfm;
/**
* Operation FD
*/
int op;
};
METHOD(af_alg_ops_t, reset, void,
private_af_alg_ops_t *this)
{
if (this->op != -1)
{
close(this->op);
this->op = -1;
}
}
METHOD(af_alg_ops_t, hash, bool,
private_af_alg_ops_t *this, chunk_t data, char *out, size_t outlen)
{
ssize_t len;
while (this->op == -1)
{
this->op = accept(this->tfm, NULL, 0);
if (this->op == -1 && errno != EINTR)
{
DBG1(DBG_LIB, "opening AF_ALG hasher failed: %s", strerror(errno));
return FALSE;
}
}
do
{
len = send(this->op, data.ptr, data.len, out ? 0 : MSG_MORE);
if (len == -1)
{
if (errno == EINTR)
{
continue;
}
DBG1(DBG_LIB, "writing to AF_ALG hasher failed: %s", strerror(errno));
return FALSE;
}
data = chunk_skip(data, len);
}
while (data.len);
if (out)
{
while (outlen)
{
len = read(this->op, out, outlen);
if (len == -1)
{
if (errno == EINTR)
{
continue;
}
DBG1(DBG_LIB, "reading AF_ALG hasher failed: %s", strerror(errno));
return FALSE;
}
outlen -= len;
out += len;
}
reset(this);
}
return TRUE;
}
METHOD(af_alg_ops_t, crypt, bool,
private_af_alg_ops_t *this, uint32_t type, chunk_t iv, chunk_t data,
char *out)
{
struct msghdr msg = {};
struct cmsghdr *cmsg;
struct af_alg_iv *ivm;
struct iovec iov;
char buf[CMSG_SPACE(sizeof(type)) +
CMSG_SPACE(offsetof(struct af_alg_iv, iv) + iv.len)];
ssize_t len;
int op;
do
{
op = accept(this->tfm, NULL, 0);
if (op == -1 && errno != EINTR)
{
DBG1(DBG_LIB, "accepting AF_ALG crypter failed: %s", strerror(errno));
return FALSE;
}
}
while (op == -1);
memset(buf, 0, sizeof(buf));
msg.msg_control = buf;
msg.msg_controllen = sizeof(buf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_OP;
cmsg->cmsg_len = CMSG_LEN(sizeof(type));
memcpy(CMSG_DATA(cmsg), &type, sizeof(type));
cmsg = CMSG_NXTHDR(&msg, cmsg);
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_IV;
cmsg->cmsg_len = CMSG_LEN(offsetof(struct af_alg_iv, iv) + iv.len);
ivm = (void*)CMSG_DATA(cmsg);
ivm->ivlen = iv.len;
memcpy(ivm->iv, iv.ptr, iv.len);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
while (data.len)
{
iov.iov_base = data.ptr;
iov.iov_len = data.len;
len = sendmsg(op, &msg, 0);
if (len == -1)
{
if (errno == EINTR)
{
continue;
}
DBG1(DBG_LIB, "writing to AF_ALG crypter failed: %s", strerror(errno));
return FALSE;
}
while (read(op, out, len) != len)
{
if (errno != EINTR)
{
DBG1(DBG_LIB, "reading from AF_ALG crypter failed: %s",
strerror(errno));
return FALSE;
}
}
data = chunk_skip(data, len);
/* no IV for subsequent data chunks */
msg.msg_controllen = 0;
}
close(op);
return TRUE;
}
METHOD(af_alg_ops_t, set_key, bool,
private_af_alg_ops_t *this, chunk_t key)
{
if (setsockopt(this->tfm, SOL_ALG, ALG_SET_KEY, key.ptr, key.len) == -1)
{
DBG1(DBG_LIB, "setting AF_ALG key failed: %s", strerror(errno));
return FALSE;
}
return TRUE;
}
METHOD(af_alg_ops_t, destroy, void,
private_af_alg_ops_t *this)
{
close(this->tfm);
if (this->op != -1)
{
close(this->op);
}
free(this);
}
/**
* See header
*/
af_alg_ops_t *af_alg_ops_create(char *type, char *alg)
{
private_af_alg_ops_t *this;
struct sockaddr_alg sa = {
.salg_family = AF_ALG,
};
strncpy(sa.salg_type, type, sizeof(sa.salg_type));
strncpy(sa.salg_name, alg, sizeof(sa.salg_name));
INIT(this,
.public = {
.hash = _hash,
.reset = _reset,
.crypt = _crypt,
.set_key = _set_key,
.destroy = _destroy,
},
.tfm = socket(AF_ALG, SOCK_SEQPACKET, 0),
.op = -1,
);
if (this->tfm == -1)
{
DBG1(DBG_LIB, "opening AF_ALG socket failed: %s", strerror(errno));
free(this);
return NULL;
}
if (bind(this->tfm, (struct sockaddr*)&sa, sizeof(sa)) == -1)
{
if (errno != ENOENT)
{ /* fail silently if algorithm not supported */
DBG1(DBG_LIB, "binding AF_ALG socket for '%s' failed: %s",
sa.salg_name, strerror(errno));
}
destroy(this);
return NULL;
}
return &this->public;
}
|