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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
/*
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* 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.
*
* $Id: crypto_factory.c 4592 2008-11-05 16:21:57Z martin $
*/
#include "crypto_factory.h"
#include <utils/linked_list.h>
#include <utils/mutex.h>
typedef struct entry_t entry_t;
struct entry_t {
/** algorithm */
u_int algo;
/* constructor */
union {
crypter_constructor_t create_crypter;
signer_constructor_t create_signer;
hasher_constructor_t create_hasher;
prf_constructor_t create_prf;
rng_constructor_t create_rng;
dh_constructor_t create_dh;
};
};
typedef struct private_crypto_factory_t private_crypto_factory_t;
/**
* private data of crypto_factory
*/
struct private_crypto_factory_t {
/**
* public functions
*/
crypto_factory_t public;
/**
* registered crypters, as entry_t
*/
linked_list_t *crypters;
/**
* registered signers, as entry_t
*/
linked_list_t *signers;
/**
* registered hashers, as entry_t
*/
linked_list_t *hashers;
/**
* registered prfs, as entry_t
*/
linked_list_t *prfs;
/**
* registered rngs, as entry_t
*/
linked_list_t *rngs;
/**
* registered diffie hellman, as entry_t
*/
linked_list_t *dhs;
/**
* rwlock to lock access to modules
*/
rwlock_t *lock;
};
/**
* Implementation of crypto_factory_t.create_crypter.
*/
static crypter_t* create_crypter(private_crypto_factory_t *this,
encryption_algorithm_t algo, size_t key_size)
{
enumerator_t *enumerator;
entry_t *entry;
crypter_t *crypter = NULL;
this->lock->read_lock(this->lock);
enumerator = this->crypters->create_enumerator(this->crypters);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->algo == algo)
{
crypter = entry->create_crypter(algo, key_size);
if (crypter)
{
break;
}
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
return crypter;
}
/**
* Implementation of crypto_factory_t.create_signer.
*/
static signer_t* create_signer(private_crypto_factory_t *this,
integrity_algorithm_t algo)
{
enumerator_t *enumerator;
entry_t *entry;
signer_t *signer = NULL;
this->lock->read_lock(this->lock);
enumerator = this->signers->create_enumerator(this->signers);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->algo == algo)
{
signer = entry->create_signer(algo);
if (signer)
{
break;
}
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
return signer;
}
/**
* Implementation of crypto_factory_t.create_hasher.
*/
static hasher_t* create_hasher(private_crypto_factory_t *this,
hash_algorithm_t algo)
{
enumerator_t *enumerator;
entry_t *entry;
hasher_t *hasher = NULL;
this->lock->read_lock(this->lock);
enumerator = this->hashers->create_enumerator(this->hashers);
while (enumerator->enumerate(enumerator, &entry))
{
if (algo == HASH_PREFERRED || entry->algo == algo)
{
hasher = entry->create_hasher(entry->algo);
if (hasher)
{
break;
}
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
return hasher;
}
/**
* Implementation of crypto_factory_t.create_prf.
*/
static prf_t* create_prf(private_crypto_factory_t *this,
pseudo_random_function_t algo)
{
enumerator_t *enumerator;
entry_t *entry;
prf_t *prf = NULL;
this->lock->read_lock(this->lock);
enumerator = this->prfs->create_enumerator(this->prfs);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->algo == algo)
{
prf = entry->create_prf(algo);
if (prf)
{
break;
}
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
return prf;
}
/**
* Implementation of crypto_factory_t.create_rng.
*/
static rng_t* create_rng(private_crypto_factory_t *this, rng_quality_t quality)
{
enumerator_t *enumerator;
entry_t *entry;
u_int diff = ~0;
rng_constructor_t constr = NULL;
this->lock->read_lock(this->lock);
enumerator = this->rngs->create_enumerator(this->rngs);
while (enumerator->enumerate(enumerator, &entry))
{ /* find the best matching quality, but at least as good as requested */
if (entry->algo >= quality && diff > entry->algo - quality)
{
diff = entry->algo - quality;
constr = entry->create_rng;
if (diff == 0)
{ /* perfect match, won't get better */
break;
}
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
if (constr)
{
return constr(quality);
}
return NULL;
}
/**
* Implementation of crypto_factory_t.create_dh.
*/
static diffie_hellman_t* create_dh(private_crypto_factory_t *this,
diffie_hellman_group_t group)
{
enumerator_t *enumerator;
entry_t *entry;
diffie_hellman_t *diffie_hellman = NULL;
this->lock->read_lock(this->lock);
enumerator = this->dhs->create_enumerator(this->dhs);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->algo == group)
{
diffie_hellman = entry->create_dh(group);
if (diffie_hellman)
{
break;
}
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
return diffie_hellman;
}
/**
* Implementation of crypto_factory_t.add_crypter.
*/
static void add_crypter(private_crypto_factory_t *this,
encryption_algorithm_t algo,
crypter_constructor_t create)
{
entry_t *entry = malloc_thing(entry_t);
entry->algo = algo;
entry->create_crypter = create;
this->lock->write_lock(this->lock);
this->crypters->insert_last(this->crypters, entry);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.remove_crypter.
*/
static void remove_crypter(private_crypto_factory_t *this,
crypter_constructor_t create)
{
entry_t *entry;
enumerator_t *enumerator;
this->lock->write_lock(this->lock);
enumerator = this->crypters->create_enumerator(this->crypters);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->create_crypter == create)
{
this->crypters->remove_at(this->crypters, enumerator);
free(entry);
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.add_signer.
*/
static void add_signer(private_crypto_factory_t *this,
integrity_algorithm_t algo, signer_constructor_t create)
{
entry_t *entry = malloc_thing(entry_t);
entry->algo = algo;
entry->create_signer = create;
this->lock->write_lock(this->lock);
this->signers->insert_last(this->signers, entry);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.remove_signer.
*/
static void remove_signer(private_crypto_factory_t *this,
signer_constructor_t create)
{
entry_t *entry;
enumerator_t *enumerator;
this->lock->write_lock(this->lock);
enumerator = this->signers->create_enumerator(this->signers);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->create_signer == create)
{
this->signers->remove_at(this->signers, enumerator);
free(entry);
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.add_hasher.
*/
static void add_hasher(private_crypto_factory_t *this, hash_algorithm_t algo,
hasher_constructor_t create)
{
entry_t *entry = malloc_thing(entry_t);
entry->algo = algo;
entry->create_hasher = create;
this->lock->write_lock(this->lock);
this->hashers->insert_last(this->hashers, entry);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.remove_hasher.
*/
static void remove_hasher(private_crypto_factory_t *this,
hasher_constructor_t create)
{
entry_t *entry;
enumerator_t *enumerator;
this->lock->write_lock(this->lock);
enumerator = this->hashers->create_enumerator(this->hashers);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->create_hasher == create)
{
this->hashers->remove_at(this->hashers, enumerator);
free(entry);
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.add_prf.
*/
static void add_prf(private_crypto_factory_t *this,
pseudo_random_function_t algo, prf_constructor_t create)
{
entry_t *entry = malloc_thing(entry_t);
entry->algo = algo;
entry->create_prf = create;
this->lock->write_lock(this->lock);
this->prfs->insert_last(this->prfs, entry);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.remove_prf.
*/
static void remove_prf(private_crypto_factory_t *this, prf_constructor_t create)
{
entry_t *entry;
enumerator_t *enumerator;
this->lock->write_lock(this->lock);
enumerator = this->prfs->create_enumerator(this->prfs);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->create_prf == create)
{
this->prfs->remove_at(this->prfs, enumerator);
free(entry);
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.add_rng.
*/
static void add_rng(private_crypto_factory_t *this, rng_quality_t quality,
rng_constructor_t create)
{
entry_t *entry = malloc_thing(entry_t);
entry->algo = quality;
entry->create_rng = create;
this->lock->write_lock(this->lock);
this->rngs->insert_last(this->rngs, entry);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.remove_rng.
*/
static void remove_rng(private_crypto_factory_t *this, rng_constructor_t create)
{
entry_t *entry;
enumerator_t *enumerator;
this->lock->write_lock(this->lock);
enumerator = this->rngs->create_enumerator(this->rngs);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->create_rng == create)
{
this->rngs->remove_at(this->rngs, enumerator);
free(entry);
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.add_dh.
*/
static void add_dh(private_crypto_factory_t *this, diffie_hellman_group_t group,
dh_constructor_t create)
{
entry_t *entry = malloc_thing(entry_t);
entry->algo = group;
entry->create_dh = create;
this->lock->write_lock(this->lock);
this->dhs->insert_last(this->dhs, entry);
this->lock->unlock(this->lock);
}
/**
* Implementation of crypto_factory_t.remove_dh.
*/
static void remove_dh(private_crypto_factory_t *this, dh_constructor_t create)
{
entry_t *entry;
enumerator_t *enumerator;
this->lock->write_lock(this->lock);
enumerator = this->dhs->create_enumerator(this->dhs);
while (enumerator->enumerate(enumerator, &entry))
{
if (entry->create_dh == create)
{
this->dhs->remove_at(this->dhs, enumerator);
free(entry);
}
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
}
/**
* match algorithms of an entry?
*/
static bool entry_match(entry_t *a, entry_t *b)
{
return a->algo == b->algo;
}
/**
* check for uniqueness of an entry
*/
static bool unique_check(linked_list_t *list, entry_t **in, entry_t **out)
{
if (list->find_first(list, (void*)entry_match, NULL, *in) == SUCCESS)
{
return FALSE;
}
*out = *in;
list->insert_last(list, *in);
return TRUE;
}
/**
* create an enumerator over entry->algo in list with locking and unique check
*/
static enumerator_t *create_enumerator(private_crypto_factory_t *this,
linked_list_t *list, void *filter)
{
this->lock->read_lock(this->lock);
return enumerator_create_filter(
enumerator_create_filter(
list->create_enumerator(list), (void*)unique_check,
linked_list_create(), (void*)list->destroy),
filter, this->lock, (void*)this->lock->unlock);
}
/**
* Filter function to enumerate algorithm, not entry
*/
static bool crypter_filter(void *n, entry_t **entry, encryption_algorithm_t *algo)
{
*algo = (*entry)->algo;
return TRUE;
}
/**
* Implementation of crypto_factory_t.create_crypter_enumerator
*/
static enumerator_t* create_crypter_enumerator(private_crypto_factory_t *this)
{
return create_enumerator(this, this->crypters, crypter_filter);
}
/**
* Filter function to enumerate algorithm, not entry
*/
static bool signer_filter(void *n, entry_t **entry, integrity_algorithm_t *algo)
{
*algo = (*entry)->algo;
return TRUE;
}
/**
* Implementation of crypto_factory_t.create_signer_enumerator
*/
static enumerator_t* create_signer_enumerator(private_crypto_factory_t *this)
{
return create_enumerator(this, this->signers, signer_filter);
}
/**
* Filter function to enumerate algorithm, not entry
*/
static bool hasher_filter(void *n, entry_t **entry, hash_algorithm_t *algo)
{
*algo = (*entry)->algo;
return TRUE;
}
/**
* Implementation of crypto_factory_t.create_hasher_enumerator
*/
static enumerator_t* create_hasher_enumerator(private_crypto_factory_t *this)
{
return create_enumerator(this, this->hashers, hasher_filter);
}
/**
* Filter function to enumerate algorithm, not entry
*/
static bool prf_filter(void *n, entry_t **entry, pseudo_random_function_t *algo)
{
*algo = (*entry)->algo;
return TRUE;
}
/**
* Implementation of crypto_factory_t.create_prf_enumerator
*/
static enumerator_t* create_prf_enumerator(private_crypto_factory_t *this)
{
return create_enumerator(this, this->prfs, prf_filter);
}
/**
* Filter function to enumerate algorithm, not entry
*/
static bool dh_filter(void *n, entry_t **entry, diffie_hellman_group_t *group)
{
*group = (*entry)->algo;
return TRUE;
}
/**
* Implementation of crypto_factory_t.create_dh_enumerator
*/
static enumerator_t* create_dh_enumerator(private_crypto_factory_t *this)
{
return create_enumerator(this, this->dhs, dh_filter);
}
/**
* Implementation of crypto_factory_t.destroy
*/
static void destroy(private_crypto_factory_t *this)
{
this->crypters->destroy_function(this->crypters, free);
this->signers->destroy_function(this->signers, free);
this->hashers->destroy_function(this->hashers, free);
this->prfs->destroy_function(this->prfs, free);
this->rngs->destroy_function(this->rngs, free);
this->dhs->destroy_function(this->dhs, free);
this->lock->destroy(this->lock);
free(this);
}
/*
* see header file
*/
crypto_factory_t *crypto_factory_create()
{
private_crypto_factory_t *this = malloc_thing(private_crypto_factory_t);
this->public.create_crypter = (crypter_t*(*)(crypto_factory_t*, encryption_algorithm_t, size_t))create_crypter;
this->public.create_signer = (signer_t*(*)(crypto_factory_t*, integrity_algorithm_t))create_signer;
this->public.create_hasher = (hasher_t*(*)(crypto_factory_t*, hash_algorithm_t))create_hasher;
this->public.create_prf = (prf_t*(*)(crypto_factory_t*, pseudo_random_function_t))create_prf;
this->public.create_rng = (rng_t*(*)(crypto_factory_t*, rng_quality_t quality))create_rng;
this->public.create_dh = (diffie_hellman_t*(*)(crypto_factory_t*, diffie_hellman_group_t group))create_dh;
this->public.add_crypter = (void(*)(crypto_factory_t*, encryption_algorithm_t algo, crypter_constructor_t create))add_crypter;
this->public.remove_crypter = (void(*)(crypto_factory_t*, crypter_constructor_t create))remove_crypter;
this->public.add_signer = (void(*)(crypto_factory_t*, integrity_algorithm_t algo, signer_constructor_t create))add_signer;
this->public.remove_signer = (void(*)(crypto_factory_t*, signer_constructor_t create))remove_signer;
this->public.add_hasher = (void(*)(crypto_factory_t*, hash_algorithm_t algo, hasher_constructor_t create))add_hasher;
this->public.remove_hasher = (void(*)(crypto_factory_t*, hasher_constructor_t create))remove_hasher;
this->public.add_prf = (void(*)(crypto_factory_t*, pseudo_random_function_t algo, prf_constructor_t create))add_prf;
this->public.remove_prf = (void(*)(crypto_factory_t*, prf_constructor_t create))remove_prf;
this->public.add_rng = (void(*)(crypto_factory_t*, rng_quality_t quality, rng_constructor_t create))add_rng;
this->public.remove_rng = (void(*)(crypto_factory_t*, rng_constructor_t create))remove_rng;
this->public.add_dh = (void(*)(crypto_factory_t*, diffie_hellman_group_t algo, dh_constructor_t create))add_dh;
this->public.remove_dh = (void(*)(crypto_factory_t*, dh_constructor_t create))remove_dh;
this->public.create_crypter_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_crypter_enumerator;
this->public.create_signer_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_signer_enumerator;
this->public.create_hasher_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_hasher_enumerator;
this->public.create_prf_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_prf_enumerator;
this->public.create_dh_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_dh_enumerator;
this->public.destroy = (void(*)(crypto_factory_t*))destroy;
this->crypters = linked_list_create();
this->signers = linked_list_create();
this->hashers = linked_list_create();
this->prfs = linked_list_create();
this->rngs = linked_list_create();
this->dhs = linked_list_create();
this->lock = rwlock_create(RWLOCK_DEFAULT);
return &this->public;
}
|