crypto-factory: Try next available RNG implementation if constructor fails

This commit is contained in:
Tobias Brunner
2013-10-11 15:13:25 +02:00
parent 2e22333fbc
commit ec91f15e3b
+6 -13
View File
@@ -235,7 +235,6 @@ METHOD(crypto_factory_t, create_signer, signer_t*,
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
return signer; return signer;
} }
@@ -308,14 +307,13 @@ METHOD(crypto_factory_t, create_rng, rng_t*,
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
entry_t *entry; entry_t *entry;
u_int diff = ~0; rng_t *rng = NULL;
rng_constructor_t constr = NULL;
this->lock->read_lock(this->lock); this->lock->read_lock(this->lock);
enumerator = this->rngs->create_enumerator(this->rngs); enumerator = this->rngs->create_enumerator(this->rngs);
while (enumerator->enumerate(enumerator, &entry)) while (enumerator->enumerate(enumerator, &entry))
{ /* find the best matching quality, but at least as good as requested */ { /* 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 && if (this->test_on_create &&
!this->tester->test_rng(this->tester, quality, !this->tester->test_rng(this->tester, quality,
@@ -324,21 +322,16 @@ METHOD(crypto_factory_t, create_rng, rng_t*,
{ {
continue; continue;
} }
diff = entry->algo - quality; rng = entry->create_rng(quality);
constr = entry->create_rng; if (rng)
if (diff == 0) {
{ /* perfect match, won't get better */
break; break;
} }
} }
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
if (constr) return rng;
{
return constr(quality);
}
return NULL;
} }
METHOD(crypto_factory_t, create_nonce_gen, nonce_gen_t*, METHOD(crypto_factory_t, create_nonce_gen, nonce_gen_t*,