Migrated crypto_factory to INIT/METHOD macros

This commit is contained in:
Martin Willi
2010-08-16 17:06:27 +02:00
parent aed2bf0bd9
commit e8bf9d6e16
2 changed files with 102 additions and 157 deletions
+99 -155
View File
@@ -98,11 +98,9 @@ struct private_crypto_factory_t {
rwlock_t *lock; rwlock_t *lock;
}; };
/** METHOD(crypto_factory_t, create_crypter, crypter_t*,
* Implementation of crypto_factory_t.create_crypter. private_crypto_factory_t *this, encryption_algorithm_t algo,
*/ size_t key_size)
static crypter_t* create_crypter(private_crypto_factory_t *this,
encryption_algorithm_t algo, size_t key_size)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
entry_t *entry; entry_t *entry;
@@ -132,11 +130,8 @@ static crypter_t* create_crypter(private_crypto_factory_t *this,
return crypter; return crypter;
} }
/** METHOD(crypto_factory_t, create_signer, signer_t*,
* Implementation of crypto_factory_t.create_signer. private_crypto_factory_t *this, integrity_algorithm_t algo)
*/
static signer_t* create_signer(private_crypto_factory_t *this,
integrity_algorithm_t algo)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
entry_t *entry; entry_t *entry;
@@ -167,11 +162,8 @@ static signer_t* create_signer(private_crypto_factory_t *this,
return signer; return signer;
} }
/** METHOD(crypto_factory_t, create_hasher, hasher_t*,
* Implementation of crypto_factory_t.create_hasher. private_crypto_factory_t *this, hash_algorithm_t algo)
*/
static hasher_t* create_hasher(private_crypto_factory_t *this,
hash_algorithm_t algo)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
entry_t *entry; entry_t *entry;
@@ -201,11 +193,8 @@ static hasher_t* create_hasher(private_crypto_factory_t *this,
return hasher; return hasher;
} }
/** METHOD(crypto_factory_t, create_prf, prf_t*,
* Implementation of crypto_factory_t.create_prf. private_crypto_factory_t *this, pseudo_random_function_t algo)
*/
static prf_t* create_prf(private_crypto_factory_t *this,
pseudo_random_function_t algo)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
entry_t *entry; entry_t *entry;
@@ -234,10 +223,8 @@ static prf_t* create_prf(private_crypto_factory_t *this,
return prf; return prf;
} }
/** METHOD(crypto_factory_t, create_rng, rng_t*,
* Implementation of crypto_factory_t.create_rng. private_crypto_factory_t *this, rng_quality_t quality)
*/
static rng_t* create_rng(private_crypto_factory_t *this, rng_quality_t quality)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
entry_t *entry; entry_t *entry;
@@ -272,11 +259,8 @@ static rng_t* create_rng(private_crypto_factory_t *this, rng_quality_t quality)
return NULL; return NULL;
} }
/** METHOD(crypto_factory_t, create_dh, diffie_hellman_t*,
* Implementation of crypto_factory_t.create_dh. private_crypto_factory_t *this, diffie_hellman_group_t group)
*/
static diffie_hellman_t* create_dh(private_crypto_factory_t *this,
diffie_hellman_group_t group)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
entry_t *entry; entry_t *entry;
@@ -300,12 +284,9 @@ static diffie_hellman_t* create_dh(private_crypto_factory_t *this,
return diffie_hellman; return diffie_hellman;
} }
/** METHOD(crypto_factory_t, add_crypter, void,
* Implementation of crypto_factory_t.add_crypter. private_crypto_factory_t *this, encryption_algorithm_t algo,
*/ crypter_constructor_t create)
static void add_crypter(private_crypto_factory_t *this,
encryption_algorithm_t algo,
crypter_constructor_t create)
{ {
if (!this->test_on_add || if (!this->test_on_add ||
this->tester->test_crypter(this->tester, algo, 0, create)) this->tester->test_crypter(this->tester, algo, 0, create))
@@ -320,11 +301,8 @@ static void add_crypter(private_crypto_factory_t *this,
} }
} }
/** METHOD(crypto_factory_t, remove_crypter, void,
* Implementation of crypto_factory_t.remove_crypter. private_crypto_factory_t *this, crypter_constructor_t create)
*/
static void remove_crypter(private_crypto_factory_t *this,
crypter_constructor_t create)
{ {
entry_t *entry; entry_t *entry;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -343,11 +321,9 @@ static void remove_crypter(private_crypto_factory_t *this,
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
} }
/** METHOD(crypto_factory_t, add_signer, void,
* Implementation of crypto_factory_t.add_signer. private_crypto_factory_t *this, integrity_algorithm_t algo,
*/ signer_constructor_t create)
static void add_signer(private_crypto_factory_t *this,
integrity_algorithm_t algo, signer_constructor_t create)
{ {
if (!this->test_on_add || if (!this->test_on_add ||
this->tester->test_signer(this->tester, algo, create)) this->tester->test_signer(this->tester, algo, create))
@@ -362,11 +338,8 @@ static void add_signer(private_crypto_factory_t *this,
} }
} }
/** METHOD(crypto_factory_t, remove_signer, void,
* Implementation of crypto_factory_t.remove_signer. private_crypto_factory_t *this, signer_constructor_t create)
*/
static void remove_signer(private_crypto_factory_t *this,
signer_constructor_t create)
{ {
entry_t *entry; entry_t *entry;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -385,11 +358,9 @@ static void remove_signer(private_crypto_factory_t *this,
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
} }
/** METHOD(crypto_factory_t, add_hasher, void,
* Implementation of crypto_factory_t.add_hasher. private_crypto_factory_t *this, hash_algorithm_t algo,
*/ hasher_constructor_t create)
static void add_hasher(private_crypto_factory_t *this, hash_algorithm_t algo,
hasher_constructor_t create)
{ {
if (!this->test_on_add || if (!this->test_on_add ||
this->tester->test_hasher(this->tester, algo, create)) this->tester->test_hasher(this->tester, algo, create))
@@ -404,11 +375,8 @@ static void add_hasher(private_crypto_factory_t *this, hash_algorithm_t algo,
} }
} }
/** METHOD(crypto_factory_t, remove_hasher, void,
* Implementation of crypto_factory_t.remove_hasher. private_crypto_factory_t *this, hasher_constructor_t create)
*/
static void remove_hasher(private_crypto_factory_t *this,
hasher_constructor_t create)
{ {
entry_t *entry; entry_t *entry;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -427,11 +395,9 @@ static void remove_hasher(private_crypto_factory_t *this,
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
} }
/** METHOD(crypto_factory_t, add_prf, void,
* Implementation of crypto_factory_t.add_prf. private_crypto_factory_t *this, pseudo_random_function_t algo,
*/ prf_constructor_t create)
static void add_prf(private_crypto_factory_t *this,
pseudo_random_function_t algo, prf_constructor_t create)
{ {
if (!this->test_on_add || if (!this->test_on_add ||
this->tester->test_prf(this->tester, algo, create)) this->tester->test_prf(this->tester, algo, create))
@@ -446,10 +412,8 @@ static void add_prf(private_crypto_factory_t *this,
} }
} }
/** METHOD(crypto_factory_t, remove_prf, void,
* Implementation of crypto_factory_t.remove_prf. private_crypto_factory_t *this, prf_constructor_t create)
*/
static void remove_prf(private_crypto_factory_t *this, prf_constructor_t create)
{ {
entry_t *entry; entry_t *entry;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -468,11 +432,9 @@ static void remove_prf(private_crypto_factory_t *this, prf_constructor_t create)
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
} }
/** METHOD(crypto_factory_t, add_rng, void,
* Implementation of crypto_factory_t.add_rng. private_crypto_factory_t *this, rng_quality_t quality,
*/ rng_constructor_t create)
static void add_rng(private_crypto_factory_t *this, rng_quality_t quality,
rng_constructor_t create)
{ {
if (!this->test_on_add || if (!this->test_on_add ||
this->tester->test_rng(this->tester, quality, create)) this->tester->test_rng(this->tester, quality, create))
@@ -487,10 +449,8 @@ static void add_rng(private_crypto_factory_t *this, rng_quality_t quality,
} }
} }
/** METHOD(crypto_factory_t, remove_rng, void,
* Implementation of crypto_factory_t.remove_rng. private_crypto_factory_t *this, rng_constructor_t create)
*/
static void remove_rng(private_crypto_factory_t *this, rng_constructor_t create)
{ {
entry_t *entry; entry_t *entry;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -509,11 +469,9 @@ static void remove_rng(private_crypto_factory_t *this, rng_constructor_t create)
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
} }
/** METHOD(crypto_factory_t, add_dh, void,
* Implementation of crypto_factory_t.add_dh. private_crypto_factory_t *this, diffie_hellman_group_t group,
*/ dh_constructor_t create)
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_t *entry = malloc_thing(entry_t);
@@ -524,10 +482,8 @@ static void add_dh(private_crypto_factory_t *this, diffie_hellman_group_t group,
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
} }
/** METHOD(crypto_factory_t, remove_dh, void,
* Implementation of crypto_factory_t.remove_dh. private_crypto_factory_t *this, dh_constructor_t create)
*/
static void remove_dh(private_crypto_factory_t *this, dh_constructor_t create)
{ {
entry_t *entry; entry_t *entry;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -591,10 +547,8 @@ static bool crypter_filter(void *n, entry_t **entry, encryption_algorithm_t *alg
return TRUE; return TRUE;
} }
/** METHOD(crypto_factory_t, create_crypter_enumerator, enumerator_t*,
* Implementation of crypto_factory_t.create_crypter_enumerator private_crypto_factory_t *this)
*/
static enumerator_t* create_crypter_enumerator(private_crypto_factory_t *this)
{ {
return create_enumerator(this, this->crypters, crypter_filter); return create_enumerator(this, this->crypters, crypter_filter);
} }
@@ -608,10 +562,8 @@ static bool signer_filter(void *n, entry_t **entry, integrity_algorithm_t *algo)
return TRUE; return TRUE;
} }
/** METHOD(crypto_factory_t, create_signer_enumerator, enumerator_t*,
* Implementation of crypto_factory_t.create_signer_enumerator private_crypto_factory_t *this)
*/
static enumerator_t* create_signer_enumerator(private_crypto_factory_t *this)
{ {
return create_enumerator(this, this->signers, signer_filter); return create_enumerator(this, this->signers, signer_filter);
} }
@@ -625,10 +577,8 @@ static bool hasher_filter(void *n, entry_t **entry, hash_algorithm_t *algo)
return TRUE; return TRUE;
} }
/** METHOD(crypto_factory_t, create_hasher_enumerator, enumerator_t*,
* Implementation of crypto_factory_t.create_hasher_enumerator private_crypto_factory_t *this)
*/
static enumerator_t* create_hasher_enumerator(private_crypto_factory_t *this)
{ {
return create_enumerator(this, this->hashers, hasher_filter); return create_enumerator(this, this->hashers, hasher_filter);
} }
@@ -642,10 +592,8 @@ static bool prf_filter(void *n, entry_t **entry, pseudo_random_function_t *algo)
return TRUE; return TRUE;
} }
/** METHOD(crypto_factory_t, create_prf_enumerator, enumerator_t*,
* Implementation of crypto_factory_t.create_prf_enumerator private_crypto_factory_t *this)
*/
static enumerator_t* create_prf_enumerator(private_crypto_factory_t *this)
{ {
return create_enumerator(this, this->prfs, prf_filter); return create_enumerator(this, this->prfs, prf_filter);
} }
@@ -659,19 +607,14 @@ static bool dh_filter(void *n, entry_t **entry, diffie_hellman_group_t *group)
return TRUE; return TRUE;
} }
/** METHOD(crypto_factory_t, create_dh_enumerator, enumerator_t*,
* Implementation of crypto_factory_t.create_dh_enumerator private_crypto_factory_t *this)
*/
static enumerator_t* create_dh_enumerator(private_crypto_factory_t *this)
{ {
return create_enumerator(this, this->dhs, dh_filter); return create_enumerator(this, this->dhs, dh_filter);
} }
/** METHOD(crypto_factory_t, add_test_vector, void,
* Implementation of crypto_factory_t.add_test_vector private_crypto_factory_t *this, transform_type_t type, void *vector)
*/
static void add_test_vector(private_crypto_factory_t *this,
transform_type_t type, void *vector)
{ {
switch (type) switch (type)
{ {
@@ -691,10 +634,8 @@ static void add_test_vector(private_crypto_factory_t *this,
} }
} }
/** METHOD(crypto_factory_t, destroy, void,
* Implementation of crypto_factory_t.destroy private_crypto_factory_t *this)
*/
static void destroy(private_crypto_factory_t *this)
{ {
this->crypters->destroy_function(this->crypters, free); this->crypters->destroy_function(this->crypters, free);
this->signers->destroy_function(this->signers, free); this->signers->destroy_function(this->signers, free);
@@ -712,46 +653,49 @@ static void destroy(private_crypto_factory_t *this)
*/ */
crypto_factory_t *crypto_factory_create() crypto_factory_t *crypto_factory_create()
{ {
private_crypto_factory_t *this = malloc_thing(private_crypto_factory_t); private_crypto_factory_t *this;
this->public.create_crypter = (crypter_t*(*)(crypto_factory_t*, encryption_algorithm_t, size_t))create_crypter; INIT(this,
this->public.create_signer = (signer_t*(*)(crypto_factory_t*, integrity_algorithm_t))create_signer; .public = {
this->public.create_hasher = (hasher_t*(*)(crypto_factory_t*, hash_algorithm_t))create_hasher; .create_crypter = _create_crypter,
this->public.create_prf = (prf_t*(*)(crypto_factory_t*, pseudo_random_function_t))create_prf; .create_signer = _create_signer,
this->public.create_rng = (rng_t*(*)(crypto_factory_t*, rng_quality_t quality))create_rng; .create_hasher = _create_hasher,
this->public.create_dh = (diffie_hellman_t*(*)(crypto_factory_t*, diffie_hellman_group_t group))create_dh; .create_prf = _create_prf,
this->public.add_crypter = (void(*)(crypto_factory_t*, encryption_algorithm_t algo, crypter_constructor_t create))add_crypter; .create_rng = _create_rng,
this->public.remove_crypter = (void(*)(crypto_factory_t*, crypter_constructor_t create))remove_crypter; .create_dh = _create_dh,
this->public.add_signer = (void(*)(crypto_factory_t*, integrity_algorithm_t algo, signer_constructor_t create))add_signer; .add_crypter = _add_crypter,
this->public.remove_signer = (void(*)(crypto_factory_t*, signer_constructor_t create))remove_signer; .remove_crypter = _remove_crypter,
this->public.add_hasher = (void(*)(crypto_factory_t*, hash_algorithm_t algo, hasher_constructor_t create))add_hasher; .add_signer = _add_signer,
this->public.remove_hasher = (void(*)(crypto_factory_t*, hasher_constructor_t create))remove_hasher; .remove_signer = _remove_signer,
this->public.add_prf = (void(*)(crypto_factory_t*, pseudo_random_function_t algo, prf_constructor_t create))add_prf; .add_hasher = _add_hasher,
this->public.remove_prf = (void(*)(crypto_factory_t*, prf_constructor_t create))remove_prf; .remove_hasher = _remove_hasher,
this->public.add_rng = (void(*)(crypto_factory_t*, rng_quality_t quality, rng_constructor_t create))add_rng; .add_prf = _add_prf,
this->public.remove_rng = (void(*)(crypto_factory_t*, rng_constructor_t create))remove_rng; .remove_prf = _remove_prf,
this->public.add_dh = (void(*)(crypto_factory_t*, diffie_hellman_group_t algo, dh_constructor_t create))add_dh; .add_rng = _add_rng,
this->public.remove_dh = (void(*)(crypto_factory_t*, dh_constructor_t create))remove_dh; .remove_rng = _remove_rng,
this->public.create_crypter_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_crypter_enumerator; .add_dh = _add_dh,
this->public.create_signer_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_signer_enumerator; .remove_dh = _remove_dh,
this->public.create_hasher_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_hasher_enumerator; .create_crypter_enumerator = _create_crypter_enumerator,
this->public.create_prf_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_prf_enumerator; .create_signer_enumerator = _create_signer_enumerator,
this->public.create_dh_enumerator = (enumerator_t*(*)(crypto_factory_t*))create_dh_enumerator; .create_hasher_enumerator = _create_hasher_enumerator,
this->public.add_test_vector = (void(*)(crypto_factory_t*, transform_type_t type, ...))add_test_vector; .create_prf_enumerator = _create_prf_enumerator,
this->public.destroy = (void(*)(crypto_factory_t*))destroy; .create_dh_enumerator = _create_dh_enumerator,
.add_test_vector = _add_test_vector,
this->crypters = linked_list_create(); .destroy = _destroy,
this->signers = linked_list_create(); },
this->hashers = linked_list_create(); .crypters = linked_list_create(),
this->prfs = linked_list_create(); .signers = linked_list_create(),
this->rngs = linked_list_create(); .hashers = linked_list_create(),
this->dhs = linked_list_create(); .prfs = linked_list_create(),
this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT); .rngs = linked_list_create(),
this->tester = crypto_tester_create(); .dhs = linked_list_create(),
this->test_on_add = lib->settings->get_bool(lib->settings, .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
"libstrongswan.crypto_test.on_add", FALSE); .tester = crypto_tester_create(),
this->test_on_create = lib->settings->get_bool(lib->settings, .test_on_add = lib->settings->get_bool(lib->settings,
"libstrongswan.crypto_test.on_create", FALSE); "libstrongswan.crypto_test.on_add", FALSE),
.test_on_create = lib->settings->get_bool(lib->settings,
"libstrongswan.crypto_test.on_create", FALSE),
);
return &this->public; return &this->public;
} }
+3 -2
View File
@@ -261,9 +261,10 @@ struct crypto_factory_t {
* Add a test vector to the crypto factory. * Add a test vector to the crypto factory.
* *
* @param type type of the test vector * @param type type of the test vector
* @param ... pointer to a test vector, defined in crypto_tester.h * @param vector pointer to a test vector, defined in crypto_tester.h
*/ */
void (*add_test_vector)(crypto_factory_t *this, transform_type_t type, ...); void (*add_test_vector)(crypto_factory_t *this, transform_type_t type,
void *vector);
/** /**
* Destroy a crypto_factory instance. * Destroy a crypto_factory instance.