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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
* Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free software Foundation, Inc.,
* 59 Temple Place, suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/crypto.h>
#include <linux/sched.h>
//#include <asm/softirq.h>
#include <sa_algorithm.h>
#define MODULE_NAME "IPSEC"
#define DEBUG 1
#undef DEBUG
#include "debug.h"
/** @file Tables of supported IPSEC algorithms.
* Has tables for digests, ciphers and compression algorithms.
*/
/*
* Algorithms supported by IPsec. These entries contain properties which
* are used in key negotiation and sa processing, and are used to verify
* that instantiated crypto transforms have correct parameters for IPsec
* purposes.
*/
/** Digests. */
static SAAlgorithm digest_alg[] = {
{
.name = "digest_null",
.info = {
.digest = {
.icv_truncbits = 0,
.icv_fullbits = 0,
}
},
.alg = {
.sadb_alg_id = SADB_X_AALG_NULL,
.sadb_alg_ivlen = 0,
.sadb_alg_minbits = 0,
.sadb_alg_maxbits = 0
}
},
{
.name = "md5",
.info = { .digest = {
.icv_truncbits = 96,
.icv_fullbits = 128,
} },
.alg = {
.sadb_alg_id = SADB_AALG_MD5HMAC,
.sadb_alg_ivlen = 0,
.sadb_alg_minbits = 128,
.sadb_alg_maxbits = 128
}
},
{
.name = "sha1",
.info = {
.digest = {
.icv_truncbits = 96,
.icv_fullbits = 160,
}
},
.alg = {
.sadb_alg_id = SADB_AALG_SHA1HMAC,
.sadb_alg_ivlen = 0,
.sadb_alg_minbits = 160,
.sadb_alg_maxbits = 160
}
},
{
.name = "sha256",
.info = {
.digest = {
.icv_truncbits = 128,
.icv_fullbits = 256,
}
},
.alg = {
.sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
.sadb_alg_ivlen = 0,
.sadb_alg_minbits = 256,
.sadb_alg_maxbits = 256
}
},
/* { */
/* .name = "ripemd160", */
/* .info = { */
/* .digest = { */
/* .icv_truncbits = 96, */
/* .icv_fullbits = 160, */
/* } */
/* }, */
/* .alg = { */
/* .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC, */
/* .sadb_alg_ivlen = 0, */
/* .sadb_alg_minbits = 160, */
/* .sadb_alg_maxbits = 160 */
/* } */
/* }, */
{ /* Terminator */ }
};
/** Ciphers. */
static SAAlgorithm cipher_alg[] = {
{
.name = "cipher_null",
.info = {
.cipher = {
.blockbits = 8,
.defkeybits = 0,
}
},
.alg = {
.sadb_alg_id = SADB_EALG_NULL,
.sadb_alg_ivlen = 0,
.sadb_alg_minbits = 0,
.sadb_alg_maxbits = 0
}
},
{
.name = "des",
.info = {
.cipher = {
.blockbits = 64,
.defkeybits = 64,
}
},
.alg = {
.sadb_alg_id = SADB_EALG_DESCBC,
.sadb_alg_ivlen = 8,
.sadb_alg_minbits = 64,
.sadb_alg_maxbits = 64
}
},
{
.name = "des3_ede",
.info = {
.cipher = {
.blockbits = 64,
.defkeybits = 192,
}
},
.alg = {
.sadb_alg_id = SADB_EALG_3DESCBC,
.sadb_alg_ivlen = 8,
.sadb_alg_minbits = 192,
.sadb_alg_maxbits = 192
}
},
/* { */
/* .name = "cast128", */ //cast5?
/* .info = { */
/* .cipher = { */
/* .blockbits = 64, */
/* .defkeybits = 128, */
/* } */
/* }, */
/* .alg = { */
/* .sadb_alg_id = SADB_X_EALG_CASTCBC, */
/* .sadb_alg_ivlen = 8, */
/* .sadb_alg_minbits = 40, */
/* .sadb_alg_maxbits = 128 */
/* } */
/* }, */
{
.name = "blowfish",
.info = {
.cipher = {
.blockbits = 64,
.defkeybits = 128,
}
},
.alg = {
.sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
.sadb_alg_ivlen = 8,
.sadb_alg_minbits = 40,
.sadb_alg_maxbits = 448
}
},
{
.name = "aes",
.info = {
.cipher = {
.blockbits = 128,
.defkeybits = 128,
}
},
.alg = {
.sadb_alg_id = SADB_X_EALG_AESCBC,
.sadb_alg_ivlen = 8,
.sadb_alg_minbits = 128,
.sadb_alg_maxbits = 256
}
},
{ /* Terminator */ }
};
/** Compressors. */
static SAAlgorithm compress_alg[] = {
{
.name = "deflate",
.info = {
.compress = {
.threshold = 90,
}
},
.alg = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
},
/* { */
/* .name = "lzs", */
/* .info = { */
/* .compress = { */
/* .threshold = 90, */
/* } */
/* }, */
/* .alg = { .sadb_alg_id = SADB_X_CALG_LZS } */
/* }, */
/* { */
/* .name = "lzjh", */
/* .info = { */
/* .compress = { */
/* .threshold = 50, */
/* } */
/* }, */
/* .alg = { .sadb_alg_id = SADB_X_CALG_LZJH } */
/* }, */
{ /* Terminator */ }
};
static SAAlgorithm *sa_algorithm_by_id(SAAlgorithm *algo, int alg_id) {
for( ; algo && algo->name; algo++){
if (algo->alg.sadb_alg_id == alg_id) {
return (algo->available ? algo : NULL);
}
}
return NULL;
}
static SAAlgorithm *sa_algorithm_by_name(SAAlgorithm *algo, char *name) {
if (!name) return NULL;
for( ; algo && algo->name; algo++){
if (strcmp(name, algo->name) == 0) {
return (algo->available ? algo : NULL);
}
}
return NULL;
}
SAAlgorithm *sa_digest_by_id(int alg_id) {
return sa_algorithm_by_id(digest_alg, alg_id);
}
SAAlgorithm *sa_cipher_by_id(int alg_id) {
return sa_algorithm_by_id(cipher_alg, alg_id);
}
SAAlgorithm *sa_compress_by_id(int alg_id) {
return sa_algorithm_by_id(compress_alg, alg_id);
}
SAAlgorithm *sa_digest_by_name(char *name) {
return sa_algorithm_by_name(digest_alg, name);
}
SAAlgorithm *sa_cipher_by_name(char *name) {
return sa_algorithm_by_name(cipher_alg, name);
}
SAAlgorithm *sa_compress_by_name(char *name) {
return sa_algorithm_by_name(compress_alg, name);
}
SAAlgorithm *sa_digest_by_index(unsigned int idx) {
return digest_alg + idx;
}
SAAlgorithm *sa_cipher_by_index(unsigned int idx) {
return cipher_alg + idx;
}
SAAlgorithm *sa_compress_by_index(unsigned int idx) {
return compress_alg + idx;
}
static void sa_algorithm_probe(SAAlgorithm *algo){
int status;
dprintf("> algo=%p\n", algo);
for( ; algo && algo->name; algo++){
dprintf("> algorithm %s...\n", algo->name);
status = crypto_alg_available(algo->name, 0);
dprintf("> algorithm %s status=%d\n",algo->name, status);
if (algo->available != status){
algo->available = status;
}
}
dprintf("<\n");
}
/** Crypto api is broken. When an unregistered algorithm is requested it
* tries to load a module of the same name. But not all algorithms are
* defined by modules of the same name.
*/
static char *crypto_modules[] = {
"aes",
//"arc4",
"blowfish",
//"cast5",
//"cast6",
"crypto_null",
"des",
//"md4",
"md5",
//"serpent",
"sha1",
"sha256",
//"sha512",
//"twofish",
NULL
};
#include <linux/kmod.h>
static void sa_module_probe(char **modules){
char **p;
dprintf(">\n");
for(p = modules; *p; p++){
dprintf("> %s\n", *p);
request_module(*p);
}
dprintf("<\n");
}
/**
* Probe for the availability of crypto algorithms, and set the available
* flag for any algorithms found on the system. This is typically called by
* pfkey during userspace SA add, update or register.
*/
void sa_algorithm_probe_all(void){
dprintf("> \n");
//BUG_ON(in_softirq());
sa_module_probe(crypto_modules);
sa_algorithm_probe(digest_alg);
sa_algorithm_probe(cipher_alg);
sa_algorithm_probe(compress_alg);
dprintf("<\n");
}
|