2 * Copyright (C) 2013-2014 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "crypto_factory.h"
19 #include <utils/debug.h>
20 #include <threading/rwlock.h>
21 #include <collections/linked_list.h>
22 #include <crypto/crypto_tester.h>
23 #include <utils/test.h>
25 const char *default_plugin_name
= "default";
27 typedef struct entry_t entry_t
;
36 * plugin that registered this algorithm
38 const char *plugin_name
;
49 crypter_constructor_t create_crypter
;
50 aead_constructor_t create_aead
;
51 signer_constructor_t create_signer
;
52 hasher_constructor_t create_hasher
;
53 prf_constructor_t create_prf
;
54 rng_constructor_t create_rng
;
55 nonce_gen_constructor_t create_nonce_gen
;
56 dh_constructor_t create_dh
;
61 typedef struct private_crypto_factory_t private_crypto_factory_t
;
64 * private data of crypto_factory
66 struct private_crypto_factory_t
{
71 crypto_factory_t
public;
74 * registered crypters, as entry_t
76 linked_list_t
*crypters
;
79 * registered aead transforms, as entry_t
84 * registered signers, as entry_t
86 linked_list_t
*signers
;
89 * registered hashers, as entry_t
91 linked_list_t
*hashers
;
94 * registered prfs, as entry_t
99 * registered rngs, as entry_t
104 * registered nonce generators, as entry_t
106 linked_list_t
*nonce_gens
;
109 * registered diffie hellman, as entry_t
114 * test manager to test crypto algorithms
116 crypto_tester_t
*tester
;
119 * whether to test algorithms during registration
124 * whether to test algorithms on each crypto primitive construction
129 * run algorithm benchmark during registration
134 * Number of failed test vectors during "add".
139 * rwlock to lock access to modules
144 METHOD(crypto_factory_t
, create_crypter
, crypter_t
*,
145 private_crypto_factory_t
*this, encryption_algorithm_t algo
,
148 enumerator_t
*enumerator
;
150 crypter_t
*crypter
= NULL
;
152 this->lock
->read_lock(this->lock
);
153 enumerator
= this->crypters
->create_enumerator(this->crypters
);
154 while (enumerator
->enumerate(enumerator
, &entry
))
156 if (entry
->algo
== algo
)
158 if (this->test_on_create
&&
159 !this->tester
->test_crypter(this->tester
, algo
, key_size
,
160 entry
->create_crypter
, NULL
,
161 default_plugin_name
))
165 crypter
= entry
->create_crypter(algo
, key_size
);
172 enumerator
->destroy(enumerator
);
173 this->lock
->unlock(this->lock
);
177 METHOD(crypto_factory_t
, create_aead
, aead_t
*,
178 private_crypto_factory_t
*this, encryption_algorithm_t algo
,
179 size_t key_size
, size_t salt_size
)
181 enumerator_t
*enumerator
;
185 this->lock
->read_lock(this->lock
);
186 enumerator
= this->aeads
->create_enumerator(this->aeads
);
187 while (enumerator
->enumerate(enumerator
, &entry
))
189 if (entry
->algo
== algo
)
191 if (this->test_on_create
&&
192 !this->tester
->test_aead(this->tester
, algo
, key_size
,
193 salt_size
, entry
->create_aead
, NULL
,
194 default_plugin_name
))
198 aead
= entry
->create_aead(algo
, key_size
, salt_size
);
205 enumerator
->destroy(enumerator
);
206 this->lock
->unlock(this->lock
);
210 METHOD(crypto_factory_t
, create_signer
, signer_t
*,
211 private_crypto_factory_t
*this, integrity_algorithm_t algo
)
213 enumerator_t
*enumerator
;
215 signer_t
*signer
= NULL
;
217 this->lock
->read_lock(this->lock
);
218 enumerator
= this->signers
->create_enumerator(this->signers
);
219 while (enumerator
->enumerate(enumerator
, &entry
))
221 if (entry
->algo
== algo
)
223 if (this->test_on_create
&&
224 !this->tester
->test_signer(this->tester
, algo
,
225 entry
->create_signer
, NULL
,
226 default_plugin_name
))
230 signer
= entry
->create_signer(algo
);
237 enumerator
->destroy(enumerator
);
238 this->lock
->unlock(this->lock
);
242 METHOD(crypto_factory_t
, create_hasher
, hasher_t
*,
243 private_crypto_factory_t
*this, hash_algorithm_t algo
)
245 enumerator_t
*enumerator
;
247 hasher_t
*hasher
= NULL
;
249 this->lock
->read_lock(this->lock
);
250 enumerator
= this->hashers
->create_enumerator(this->hashers
);
251 while (enumerator
->enumerate(enumerator
, &entry
))
253 if (entry
->algo
== algo
)
255 if (this->test_on_create
&&
256 !this->tester
->test_hasher(this->tester
, algo
,
257 entry
->create_hasher
, NULL
,
258 default_plugin_name
))
262 hasher
= entry
->create_hasher(entry
->algo
);
269 enumerator
->destroy(enumerator
);
270 this->lock
->unlock(this->lock
);
274 METHOD(crypto_factory_t
, create_prf
, prf_t
*,
275 private_crypto_factory_t
*this, pseudo_random_function_t algo
)
277 enumerator_t
*enumerator
;
281 this->lock
->read_lock(this->lock
);
282 enumerator
= this->prfs
->create_enumerator(this->prfs
);
283 while (enumerator
->enumerate(enumerator
, &entry
))
285 if (entry
->algo
== algo
)
287 if (this->test_on_create
&&
288 !this->tester
->test_prf(this->tester
, algo
,
289 entry
->create_prf
, NULL
,
290 default_plugin_name
))
294 prf
= entry
->create_prf(algo
);
301 enumerator
->destroy(enumerator
);
302 this->lock
->unlock(this->lock
);
306 METHOD(crypto_factory_t
, create_rng
, rng_t
*,
307 private_crypto_factory_t
*this, rng_quality_t quality
)
309 enumerator_t
*enumerator
;
313 this->lock
->read_lock(this->lock
);
314 enumerator
= this->rngs
->create_enumerator(this->rngs
);
315 while (enumerator
->enumerate(enumerator
, &entry
))
316 { /* find the best matching quality, but at least as good as requested */
317 if (entry
->algo
>= quality
)
319 if (this->test_on_create
&&
320 !this->tester
->test_rng(this->tester
, quality
,
321 entry
->create_rng
, NULL
,
322 default_plugin_name
))
326 rng
= entry
->create_rng(quality
);
333 enumerator
->destroy(enumerator
);
334 this->lock
->unlock(this->lock
);
338 METHOD(crypto_factory_t
, create_nonce_gen
, nonce_gen_t
*,
339 private_crypto_factory_t
*this)
341 enumerator_t
*enumerator
;
343 nonce_gen_t
*nonce_gen
= NULL
;
345 this->lock
->read_lock(this->lock
);
346 enumerator
= this->nonce_gens
->create_enumerator(this->nonce_gens
);
347 while (enumerator
->enumerate(enumerator
, &entry
))
349 nonce_gen
= entry
->create_nonce_gen();
355 enumerator
->destroy(enumerator
);
356 this->lock
->unlock(this->lock
);
361 METHOD(crypto_factory_t
, create_dh
, diffie_hellman_t
*,
362 private_crypto_factory_t
*this, diffie_hellman_group_t group
, ...)
364 enumerator_t
*enumerator
;
367 chunk_t g
= chunk_empty
, p
= chunk_empty
;
368 diffie_hellman_t
*diffie_hellman
= NULL
;
370 if (group
== MODP_CUSTOM
)
372 va_start(args
, group
);
373 g
= va_arg(args
, chunk_t
);
374 p
= va_arg(args
, chunk_t
);
378 this->lock
->read_lock(this->lock
);
379 enumerator
= this->dhs
->create_enumerator(this->dhs
);
380 while (enumerator
->enumerate(enumerator
, &entry
))
382 if (entry
->algo
== group
)
384 if (this->test_on_create
&& group
!= MODP_CUSTOM
&&
385 !this->tester
->test_dh(this->tester
, group
,
386 entry
->create_dh
, NULL
, default_plugin_name
))
390 diffie_hellman
= entry
->create_dh(group
, g
, p
);
397 enumerator
->destroy(enumerator
);
398 this->lock
->unlock(this->lock
);
399 return diffie_hellman
;
403 * Insert an algorithm entry to a list
405 * Entries maintain the order in which algorithms were added, unless they were
406 * benchmarked and speed is provided, which then is used to order entries of
407 * the same algorithm.
408 * An exception are RNG entries, which are sorted by algorithm identifier.
410 static void add_entry(private_crypto_factory_t
*this, linked_list_t
*list
,
411 int algo
, const char *plugin_name
,
412 u_int speed
, void *create
)
414 enumerator_t
*enumerator
;
415 entry_t
*entry
, *current
;
416 bool sort
= (list
== this->rngs
), found
= FALSE
;
420 .plugin_name
= plugin_name
,
423 entry
->create
= create
;
425 this->lock
->write_lock(this->lock
);
426 enumerator
= list
->create_enumerator(list
);
427 while (enumerator
->enumerate(enumerator
, ¤t
))
429 if (sort
&& current
->algo
> algo
)
433 else if (current
->algo
== algo
)
435 if (speed
> current
->speed
)
446 list
->insert_before(list
, enumerator
, entry
);
447 enumerator
->destroy(enumerator
);
448 this->lock
->unlock(this->lock
);
451 METHOD(crypto_factory_t
, add_crypter
, bool,
452 private_crypto_factory_t
*this, encryption_algorithm_t algo
, size_t key_size
,
453 const char *plugin_name
, crypter_constructor_t create
)
457 if (!this->test_on_add
||
458 this->tester
->test_crypter(this->tester
, algo
, key_size
, create
,
459 this->bench ?
&speed
: NULL
, plugin_name
))
461 add_entry(this, this->crypters
, algo
, plugin_name
, speed
, create
);
464 this->test_failures
++;
468 METHOD(crypto_factory_t
, remove_crypter
, void,
469 private_crypto_factory_t
*this, crypter_constructor_t create
)
472 enumerator_t
*enumerator
;
474 this->lock
->write_lock(this->lock
);
475 enumerator
= this->crypters
->create_enumerator(this->crypters
);
476 while (enumerator
->enumerate(enumerator
, &entry
))
478 if (entry
->create_crypter
== create
)
480 this->crypters
->remove_at(this->crypters
, enumerator
);
484 enumerator
->destroy(enumerator
);
485 this->lock
->unlock(this->lock
);
488 METHOD(crypto_factory_t
, add_aead
, bool,
489 private_crypto_factory_t
*this, encryption_algorithm_t algo
, size_t key_size
,
490 const char *plugin_name
, aead_constructor_t create
)
494 if (!this->test_on_add
||
495 this->tester
->test_aead(this->tester
, algo
, key_size
, 0, create
,
496 this->bench ?
&speed
: NULL
, plugin_name
))
498 add_entry(this, this->aeads
, algo
, plugin_name
, speed
, create
);
501 this->test_failures
++;
505 METHOD(crypto_factory_t
, remove_aead
, void,
506 private_crypto_factory_t
*this, aead_constructor_t create
)
509 enumerator_t
*enumerator
;
511 this->lock
->write_lock(this->lock
);
512 enumerator
= this->aeads
->create_enumerator(this->aeads
);
513 while (enumerator
->enumerate(enumerator
, &entry
))
515 if (entry
->create_aead
== create
)
517 this->aeads
->remove_at(this->aeads
, enumerator
);
521 enumerator
->destroy(enumerator
);
522 this->lock
->unlock(this->lock
);
525 METHOD(crypto_factory_t
, add_signer
, bool,
526 private_crypto_factory_t
*this, integrity_algorithm_t algo
,
527 const char *plugin_name
, signer_constructor_t create
)
531 if (!this->test_on_add
||
532 this->tester
->test_signer(this->tester
, algo
, create
,
533 this->bench ?
&speed
: NULL
, plugin_name
))
535 add_entry(this, this->signers
, algo
, plugin_name
, speed
, create
);
538 this->test_failures
++;
542 METHOD(crypto_factory_t
, remove_signer
, void,
543 private_crypto_factory_t
*this, signer_constructor_t create
)
546 enumerator_t
*enumerator
;
548 this->lock
->write_lock(this->lock
);
549 enumerator
= this->signers
->create_enumerator(this->signers
);
550 while (enumerator
->enumerate(enumerator
, &entry
))
552 if (entry
->create_signer
== create
)
554 this->signers
->remove_at(this->signers
, enumerator
);
558 enumerator
->destroy(enumerator
);
559 this->lock
->unlock(this->lock
);
562 METHOD(crypto_factory_t
, add_hasher
, bool,
563 private_crypto_factory_t
*this, hash_algorithm_t algo
,
564 const char *plugin_name
, hasher_constructor_t create
)
568 if (!this->test_on_add
||
569 this->tester
->test_hasher(this->tester
, algo
, create
,
570 this->bench ?
&speed
: NULL
, plugin_name
))
572 add_entry(this, this->hashers
, algo
, plugin_name
, speed
, create
);
575 this->test_failures
++;
579 METHOD(crypto_factory_t
, remove_hasher
, void,
580 private_crypto_factory_t
*this, hasher_constructor_t create
)
583 enumerator_t
*enumerator
;
585 this->lock
->write_lock(this->lock
);
586 enumerator
= this->hashers
->create_enumerator(this->hashers
);
587 while (enumerator
->enumerate(enumerator
, &entry
))
589 if (entry
->create_hasher
== create
)
591 this->hashers
->remove_at(this->hashers
, enumerator
);
595 enumerator
->destroy(enumerator
);
596 this->lock
->unlock(this->lock
);
599 METHOD(crypto_factory_t
, add_prf
, bool,
600 private_crypto_factory_t
*this, pseudo_random_function_t algo
,
601 const char *plugin_name
, prf_constructor_t create
)
605 if (!this->test_on_add
||
606 this->tester
->test_prf(this->tester
, algo
, create
,
607 this->bench ?
&speed
: NULL
, plugin_name
))
609 add_entry(this, this->prfs
, algo
, plugin_name
, speed
, create
);
612 this->test_failures
++;
616 METHOD(crypto_factory_t
, remove_prf
, void,
617 private_crypto_factory_t
*this, prf_constructor_t create
)
620 enumerator_t
*enumerator
;
622 this->lock
->write_lock(this->lock
);
623 enumerator
= this->prfs
->create_enumerator(this->prfs
);
624 while (enumerator
->enumerate(enumerator
, &entry
))
626 if (entry
->create_prf
== create
)
628 this->prfs
->remove_at(this->prfs
, enumerator
);
632 enumerator
->destroy(enumerator
);
633 this->lock
->unlock(this->lock
);
636 METHOD(crypto_factory_t
, add_rng
, bool,
637 private_crypto_factory_t
*this, rng_quality_t quality
,
638 const char *plugin_name
, rng_constructor_t create
)
642 if (!this->test_on_add
||
643 this->tester
->test_rng(this->tester
, quality
, create
,
644 this->bench ?
&speed
: NULL
, plugin_name
))
646 add_entry(this, this->rngs
, quality
, plugin_name
, speed
, create
);
649 this->test_failures
++;
653 METHOD(crypto_factory_t
, remove_rng
, void,
654 private_crypto_factory_t
*this, rng_constructor_t create
)
657 enumerator_t
*enumerator
;
659 this->lock
->write_lock(this->lock
);
660 enumerator
= this->rngs
->create_enumerator(this->rngs
);
661 while (enumerator
->enumerate(enumerator
, &entry
))
663 if (entry
->create_rng
== create
)
665 this->rngs
->remove_at(this->rngs
, enumerator
);
669 enumerator
->destroy(enumerator
);
670 this->lock
->unlock(this->lock
);
673 METHOD(crypto_factory_t
, add_nonce_gen
, bool,
674 private_crypto_factory_t
*this, const char *plugin_name
,
675 nonce_gen_constructor_t create
)
677 add_entry(this, this->nonce_gens
, 0, plugin_name
, 0, create
);
681 METHOD(crypto_factory_t
, remove_nonce_gen
, void,
682 private_crypto_factory_t
*this, nonce_gen_constructor_t create
)
685 enumerator_t
*enumerator
;
687 this->lock
->write_lock(this->lock
);
688 enumerator
= this->nonce_gens
->create_enumerator(this->nonce_gens
);
689 while (enumerator
->enumerate(enumerator
, &entry
))
691 if (entry
->create_nonce_gen
== create
)
693 this->nonce_gens
->remove_at(this->nonce_gens
, enumerator
);
697 enumerator
->destroy(enumerator
);
698 this->lock
->unlock(this->lock
);
701 METHOD(crypto_factory_t
, add_dh
, bool,
702 private_crypto_factory_t
*this, diffie_hellman_group_t group
,
703 const char *plugin_name
, dh_constructor_t create
)
707 if (!this->test_on_add
||
708 this->tester
->test_dh(this->tester
, group
, create
,
709 this->bench ?
&speed
: NULL
, plugin_name
))
711 add_entry(this, this->dhs
, group
, plugin_name
, 0, create
);
714 this->test_failures
++;
718 METHOD(crypto_factory_t
, remove_dh
, void,
719 private_crypto_factory_t
*this, dh_constructor_t create
)
722 enumerator_t
*enumerator
;
724 this->lock
->write_lock(this->lock
);
725 enumerator
= this->dhs
->create_enumerator(this->dhs
);
726 while (enumerator
->enumerate(enumerator
, &entry
))
728 if (entry
->create_dh
== create
)
730 this->dhs
->remove_at(this->dhs
, enumerator
);
734 enumerator
->destroy(enumerator
);
735 this->lock
->unlock(this->lock
);
739 * match algorithms of an entry?
741 static bool entry_match(entry_t
*a
, entry_t
*b
)
743 return a
->algo
== b
->algo
;
747 * check for uniqueness of an entry
749 static bool unique_check(linked_list_t
*list
, entry_t
**in
, entry_t
**out
)
751 if (list
->find_first(list
, (void*)entry_match
, NULL
, *in
) == SUCCESS
)
756 list
->insert_last(list
, *in
);
761 * create an enumerator over entry->algo in list with locking and unique check
763 static enumerator_t
*create_enumerator(private_crypto_factory_t
*this,
764 linked_list_t
*list
, void *filter
)
766 this->lock
->read_lock(this->lock
);
767 return enumerator_create_filter(
768 enumerator_create_filter(
769 list
->create_enumerator(list
), (void*)unique_check
,
770 linked_list_create(), (void*)list
->destroy
),
771 filter
, this->lock
, (void*)this->lock
->unlock
);
775 * Filter function to enumerate algorithm, not entry
777 static bool crypter_filter(void *n
, entry_t
**entry
, encryption_algorithm_t
*algo
,
778 void *i2
, const char **plugin_name
)
780 *algo
= (*entry
)->algo
;
781 *plugin_name
= (*entry
)->plugin_name
;
785 METHOD(crypto_factory_t
, create_crypter_enumerator
, enumerator_t
*,
786 private_crypto_factory_t
*this)
788 return create_enumerator(this, this->crypters
, crypter_filter
);
791 METHOD(crypto_factory_t
, create_aead_enumerator
, enumerator_t
*,
792 private_crypto_factory_t
*this)
794 return create_enumerator(this, this->aeads
, crypter_filter
);
798 * Filter function to enumerate algorithm, not entry
800 static bool signer_filter(void *n
, entry_t
**entry
, integrity_algorithm_t
*algo
,
801 void *i2
, const char **plugin_name
)
803 *algo
= (*entry
)->algo
;
804 *plugin_name
= (*entry
)->plugin_name
;
808 METHOD(crypto_factory_t
, create_signer_enumerator
, enumerator_t
*,
809 private_crypto_factory_t
*this)
811 return create_enumerator(this, this->signers
, signer_filter
);
815 * Filter function to enumerate algorithm, not entry
817 static bool hasher_filter(void *n
, entry_t
**entry
, hash_algorithm_t
*algo
,
818 void *i2
, const char **plugin_name
)
820 *algo
= (*entry
)->algo
;
821 *plugin_name
= (*entry
)->plugin_name
;
825 METHOD(crypto_factory_t
, create_hasher_enumerator
, enumerator_t
*,
826 private_crypto_factory_t
*this)
828 return create_enumerator(this, this->hashers
, hasher_filter
);
832 * Filter function to enumerate algorithm, not entry
834 static bool prf_filter(void *n
, entry_t
**entry
, pseudo_random_function_t
*algo
,
835 void *i2
, const char **plugin_name
)
837 *algo
= (*entry
)->algo
;
838 *plugin_name
= (*entry
)->plugin_name
;
842 METHOD(crypto_factory_t
, create_prf_enumerator
, enumerator_t
*,
843 private_crypto_factory_t
*this)
845 return create_enumerator(this, this->prfs
, prf_filter
);
849 * Filter function to enumerate group, not entry
851 static bool dh_filter(void *n
, entry_t
**entry
, diffie_hellman_group_t
*group
,
852 void *i2
, const char **plugin_name
)
854 *group
= (*entry
)->algo
;
855 *plugin_name
= (*entry
)->plugin_name
;
859 METHOD(crypto_factory_t
, create_dh_enumerator
, enumerator_t
*,
860 private_crypto_factory_t
*this)
862 return create_enumerator(this, this->dhs
, dh_filter
);
866 * Filter function to enumerate strength, not entry
868 static bool rng_filter(void *n
, entry_t
**entry
, rng_quality_t
*quality
,
869 void *i2
, const char **plugin_name
)
871 *quality
= (*entry
)->algo
;
872 *plugin_name
= (*entry
)->plugin_name
;
876 METHOD(crypto_factory_t
, create_rng_enumerator
, enumerator_t
*,
877 private_crypto_factory_t
*this)
879 return create_enumerator(this, this->rngs
, rng_filter
);
883 * Filter function to enumerate plugin name, not entry
885 static bool nonce_gen_filter(void *n
, entry_t
**entry
, const char **plugin_name
)
887 *plugin_name
= (*entry
)->plugin_name
;
891 METHOD(crypto_factory_t
, create_nonce_gen_enumerator
, enumerator_t
*,
892 private_crypto_factory_t
*this)
894 return create_enumerator(this, this->nonce_gens
, nonce_gen_filter
);
897 METHOD(crypto_factory_t
, add_test_vector
, void,
898 private_crypto_factory_t
*this, transform_type_t type
, void *vector
)
902 case ENCRYPTION_ALGORITHM
:
903 return this->tester
->add_crypter_vector(this->tester
, vector
);
905 return this->tester
->add_aead_vector(this->tester
, vector
);
906 case INTEGRITY_ALGORITHM
:
907 return this->tester
->add_signer_vector(this->tester
, vector
);
909 return this->tester
->add_hasher_vector(this->tester
, vector
);
910 case PSEUDO_RANDOM_FUNCTION
:
911 return this->tester
->add_prf_vector(this->tester
, vector
);
912 case RANDOM_NUMBER_GENERATOR
:
913 return this->tester
->add_rng_vector(this->tester
, vector
);
914 case DIFFIE_HELLMAN_GROUP
:
915 return this->tester
->add_dh_vector(this->tester
, vector
);
917 DBG1(DBG_LIB
, "%N test vectors not supported, ignored",
918 transform_type_names
, type
);
923 * Private enumerator for create_verify_enumerator()
928 transform_type_t type
;
929 crypto_tester_t
*tester
;
931 } verify_enumerator_t
;
933 METHOD(enumerator_t
, verify_enumerate
, bool,
934 verify_enumerator_t
*this, u_int
*alg
, const char **plugin
, bool *valid
)
938 if (!this->inner
->enumerate(this->inner
, &entry
))
944 case ENCRYPTION_ALGORITHM
:
945 *valid
= this->tester
->test_crypter(this->tester
, entry
->algo
, 0,
946 entry
->create_crypter
, NULL
, entry
->plugin_name
);
949 *valid
= this->tester
->test_aead(this->tester
, entry
->algo
, 0, 0,
950 entry
->create_aead
, NULL
, entry
->plugin_name
);
952 case INTEGRITY_ALGORITHM
:
953 *valid
= this->tester
->test_signer(this->tester
, entry
->algo
,
954 entry
->create_signer
, NULL
, entry
->plugin_name
);
957 *valid
= this->tester
->test_hasher(this->tester
, entry
->algo
,
958 entry
->create_hasher
, NULL
, entry
->plugin_name
);
960 case PSEUDO_RANDOM_FUNCTION
:
961 *valid
= this->tester
->test_prf(this->tester
, entry
->algo
,
962 entry
->create_prf
, NULL
, entry
->plugin_name
);
964 case RANDOM_NUMBER_GENERATOR
:
965 *valid
= this->tester
->test_rng(this->tester
, entry
->algo
,
966 entry
->create_rng
, NULL
, entry
->plugin_name
);
968 case DIFFIE_HELLMAN_GROUP
:
969 *valid
= this->tester
->test_dh(this->tester
, entry
->algo
,
970 entry
->create_dh
, NULL
, entry
->plugin_name
);
975 *plugin
= entry
->plugin_name
;
980 METHOD(enumerator_t
, verify_destroy
, void,
981 verify_enumerator_t
*this)
983 this->inner
->destroy(this->inner
);
984 this->lock
->unlock(this->lock
);
988 METHOD(crypto_factory_t
, create_verify_enumerator
, enumerator_t
*,
989 private_crypto_factory_t
*this, transform_type_t type
)
991 verify_enumerator_t
*enumerator
;
994 this->lock
->read_lock(this->lock
);
997 case ENCRYPTION_ALGORITHM
:
998 inner
= this->crypters
->create_enumerator(this->crypters
);
1000 case AEAD_ALGORITHM
:
1001 inner
= this->aeads
->create_enumerator(this->aeads
);
1003 case INTEGRITY_ALGORITHM
:
1004 inner
= this->signers
->create_enumerator(this->signers
);
1006 case HASH_ALGORITHM
:
1007 inner
= this->hashers
->create_enumerator(this->hashers
);
1009 case PSEUDO_RANDOM_FUNCTION
:
1010 inner
= this->prfs
->create_enumerator(this->prfs
);
1012 case RANDOM_NUMBER_GENERATOR
:
1013 inner
= this->rngs
->create_enumerator(this->rngs
);
1015 case DIFFIE_HELLMAN_GROUP
:
1016 inner
= this->dhs
->create_enumerator(this->dhs
);
1019 this->lock
->unlock(this->lock
);
1020 return enumerator_create_empty();
1024 .enumerate
= (void*)_verify_enumerate
,
1025 .destroy
= _verify_destroy
,
1029 .tester
= this->tester
,
1032 return &enumerator
->public;
1035 METHOD(crypto_factory_t
, destroy
, void,
1036 private_crypto_factory_t
*this)
1038 this->crypters
->destroy(this->crypters
);
1039 this->aeads
->destroy(this->aeads
);
1040 this->signers
->destroy(this->signers
);
1041 this->hashers
->destroy(this->hashers
);
1042 this->prfs
->destroy(this->prfs
);
1043 this->rngs
->destroy(this->rngs
);
1044 this->nonce_gens
->destroy(this->nonce_gens
);
1045 this->dhs
->destroy(this->dhs
);
1046 this->tester
->destroy(this->tester
);
1047 this->lock
->destroy(this->lock
);
1054 crypto_factory_t
*crypto_factory_create()
1056 private_crypto_factory_t
*this;
1060 .create_crypter
= _create_crypter
,
1061 .create_aead
= _create_aead
,
1062 .create_signer
= _create_signer
,
1063 .create_hasher
= _create_hasher
,
1064 .create_prf
= _create_prf
,
1065 .create_rng
= _create_rng
,
1066 .create_nonce_gen
= _create_nonce_gen
,
1067 .create_dh
= _create_dh
,
1068 .add_crypter
= _add_crypter
,
1069 .remove_crypter
= _remove_crypter
,
1070 .add_aead
= _add_aead
,
1071 .remove_aead
= _remove_aead
,
1072 .add_signer
= _add_signer
,
1073 .remove_signer
= _remove_signer
,
1074 .add_hasher
= _add_hasher
,
1075 .remove_hasher
= _remove_hasher
,
1076 .add_prf
= _add_prf
,
1077 .remove_prf
= _remove_prf
,
1078 .add_rng
= _add_rng
,
1079 .remove_rng
= _remove_rng
,
1080 .add_nonce_gen
= _add_nonce_gen
,
1081 .remove_nonce_gen
= _remove_nonce_gen
,
1083 .remove_dh
= _remove_dh
,
1084 .create_crypter_enumerator
= _create_crypter_enumerator
,
1085 .create_aead_enumerator
= _create_aead_enumerator
,
1086 .create_signer_enumerator
= _create_signer_enumerator
,
1087 .create_hasher_enumerator
= _create_hasher_enumerator
,
1088 .create_prf_enumerator
= _create_prf_enumerator
,
1089 .create_dh_enumerator
= _create_dh_enumerator
,
1090 .create_rng_enumerator
= _create_rng_enumerator
,
1091 .create_nonce_gen_enumerator
= _create_nonce_gen_enumerator
,
1092 .add_test_vector
= _add_test_vector
,
1093 .create_verify_enumerator
= _create_verify_enumerator
,
1094 .destroy
= _destroy
,
1096 .crypters
= linked_list_create(),
1097 .aeads
= linked_list_create(),
1098 .signers
= linked_list_create(),
1099 .hashers
= linked_list_create(),
1100 .prfs
= linked_list_create(),
1101 .rngs
= linked_list_create(),
1102 .nonce_gens
= linked_list_create(),
1103 .dhs
= linked_list_create(),
1104 .lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
1105 .tester
= crypto_tester_create(),
1106 .test_on_add
= lib
->settings
->get_bool(lib
->settings
,
1107 "%s.crypto_test.on_add", FALSE
, lib
->ns
),
1108 .test_on_create
= lib
->settings
->get_bool(lib
->settings
,
1109 "%s.crypto_test.on_create", FALSE
, lib
->ns
),
1110 .bench
= lib
->settings
->get_bool(lib
->settings
,
1111 "%s.crypto_test.bench", FALSE
, lib
->ns
),
1114 return &this->public;