diff options
author | Yves-Alexis Perez <corsac@debian.org> | 2013-11-01 13:32:07 +0100 |
---|---|---|
committer | Yves-Alexis Perez <corsac@debian.org> | 2013-11-01 13:32:07 +0100 |
commit | 5313d2d78ca150515f7f5eb39801c100690b6b29 (patch) | |
tree | c78e420367283bb1b16f14210b12687cdfbd26eb /src/libstrongswan/crypto/crypto_factory.c | |
parent | 6b99c8d9cff7b3e8ae8f3204b99e7ea40f791349 (diff) | |
download | vyos-strongswan-5313d2d78ca150515f7f5eb39801c100690b6b29.tar.gz vyos-strongswan-5313d2d78ca150515f7f5eb39801c100690b6b29.zip |
Imported Upstream version 5.1.1
Diffstat (limited to 'src/libstrongswan/crypto/crypto_factory.c')
-rw-r--r-- | src/libstrongswan/crypto/crypto_factory.c | 61 |
1 files changed, 25 insertions, 36 deletions
diff --git a/src/libstrongswan/crypto/crypto_factory.c b/src/libstrongswan/crypto/crypto_factory.c index b89198003..edcabfe58 100644 --- a/src/libstrongswan/crypto/crypto_factory.c +++ b/src/libstrongswan/crypto/crypto_factory.c @@ -1,4 +1,5 @@ /* + * Copyright (C) 2013 Tobias Brunner * Copyright (C) 2008 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -234,7 +235,6 @@ METHOD(crypto_factory_t, create_signer, signer_t*, } enumerator->destroy(enumerator); this->lock->unlock(this->lock); - return signer; } @@ -249,9 +249,9 @@ METHOD(crypto_factory_t, create_hasher, hasher_t*, enumerator = this->hashers->create_enumerator(this->hashers); while (enumerator->enumerate(enumerator, &entry)) { - if (algo == HASH_PREFERRED || entry->algo == algo) + if (entry->algo == algo) { - if (this->test_on_create && algo != HASH_PREFERRED && + if (this->test_on_create && !this->tester->test_hasher(this->tester, algo, entry->create_hasher, NULL, default_plugin_name)) @@ -307,14 +307,13 @@ METHOD(crypto_factory_t, create_rng, rng_t*, { enumerator_t *enumerator; entry_t *entry; - u_int diff = ~0; - rng_constructor_t constr = NULL; + rng_t *rng = 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) + if (entry->algo >= quality) { if (this->test_on_create && !this->tester->test_rng(this->tester, quality, @@ -323,21 +322,16 @@ METHOD(crypto_factory_t, create_rng, rng_t*, { continue; } - diff = entry->algo - quality; - constr = entry->create_rng; - if (diff == 0) - { /* perfect match, won't get better */ + rng = entry->create_rng(quality); + if (rng) + { break; } } } enumerator->destroy(enumerator); this->lock->unlock(this->lock); - if (constr) - { - return constr(quality); - } - return NULL; + return rng; } METHOD(crypto_factory_t, create_nonce_gen, nonce_gen_t*, @@ -396,14 +390,18 @@ METHOD(crypto_factory_t, create_dh, diffie_hellman_t*, /** * Insert an algorithm entry to a list + * + * Entries are sorted by algorithm identifier (which is important for RNGs) + * while maintaining the order in which algorithms were added, unless they were + * benchmarked and speed is provided, which then is used to order entries of + * the same algorithm. */ static void add_entry(private_crypto_factory_t *this, linked_list_t *list, int algo, const char *plugin_name, u_int speed, void *create) { + enumerator_t *enumerator; entry_t *entry, *current; - linked_list_t *tmp; - bool inserted = FALSE; INIT(entry, .algo = algo, @@ -413,30 +411,21 @@ static void add_entry(private_crypto_factory_t *this, linked_list_t *list, entry->create = create; this->lock->write_lock(this->lock); - if (speed) - { /* insert sorted by speed using a temporary list */ - tmp = linked_list_create(); - while (list->remove_first(list, (void**)¤t) == SUCCESS) + enumerator = list->create_enumerator(list); + while (enumerator->enumerate(enumerator, ¤t)) + { + if (current->algo > algo) { - tmp->insert_last(tmp, current); + break; } - while (tmp->remove_first(tmp, (void**)¤t) == SUCCESS) + else if (current->algo == algo && speed && + current->speed < speed) { - if (!inserted && - current->algo == algo && - current->speed < speed) - { - list->insert_last(list, entry); - inserted = TRUE; - } - list->insert_last(list, current); + break; } - tmp->destroy(tmp); - } - if (!inserted) - { - list->insert_last(list, entry); } + list->insert_before(list, enumerator, entry); + enumerator->destroy(enumerator); this->lock->unlock(this->lock); } |