crypto-tester: Use the plugin feature key size to benchmark crypters/aeads
authorMartin Willi <martin@revosec.ch>
Thu, 26 Mar 2015 10:50:28 +0000 (11:50 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 15 Apr 2015 09:35:26 +0000 (11:35 +0200)
We previously didn't pass the key size during algorithm registration, but this
resulted in benchmarking with the "default" key size the crypter uses when
passing 0 as key size.

src/libstrongswan/crypto/crypto_factory.c
src/libstrongswan/crypto/crypto_factory.h
src/libstrongswan/crypto/crypto_tester.c
src/libstrongswan/plugins/plugin_feature.c

index 96fbc0d..1bc1ac7 100644 (file)
@@ -439,14 +439,14 @@ static void add_entry(private_crypto_factory_t *this, linked_list_t *list,
 }
 
 METHOD(crypto_factory_t, add_crypter, bool,
-       private_crypto_factory_t *this, encryption_algorithm_t algo,
+       private_crypto_factory_t *this, encryption_algorithm_t algo, size_t key_size,
        const char *plugin_name, crypter_constructor_t create)
 {
        u_int speed = 0;
 
        if (!this->test_on_add ||
-               this->tester->test_crypter(this->tester, algo, 0, create,
-                                                                  this->bench ? &speed : NULL, plugin_name))
+               this->tester->test_crypter(this->tester, algo, key_size, create,
+                                                                  this->bench ? &speed : NULL, plugin_name))
        {
                add_entry(this, this->crypters, algo, plugin_name, speed, create);
                return TRUE;
@@ -476,13 +476,13 @@ METHOD(crypto_factory_t, remove_crypter, void,
 }
 
 METHOD(crypto_factory_t, add_aead, bool,
-       private_crypto_factory_t *this, encryption_algorithm_t algo,
+       private_crypto_factory_t *this, encryption_algorithm_t algo, size_t key_size,
        const char *plugin_name, aead_constructor_t create)
 {
        u_int speed = 0;
 
        if (!this->test_on_add ||
-               this->tester->test_aead(this->tester, algo, 0, 0, create,
+               this->tester->test_aead(this->tester, algo, key_size, 0, create,
                                                                this->bench ? &speed : NULL, plugin_name))
        {
                add_entry(this, this->aeads, algo, plugin_name, speed, create);
index 7865bcb..b1e18df 100644 (file)
@@ -162,12 +162,14 @@ struct crypto_factory_t {
         * Register a crypter constructor.
         *
         * @param algo                  algorithm to constructor
+        * @param key size              key size to peform benchmarking for
         * @param plugin_name   plugin that registered this algorithm
         * @param create                constructor function for that algorithm
         * @return                              TRUE if registered, FALSE if test vector failed
         */
        bool (*add_crypter)(crypto_factory_t *this, encryption_algorithm_t algo,
-                                               const char *plugin_name, crypter_constructor_t create);
+                                               size_t key_size, const char *plugin_name,
+                                               crypter_constructor_t create);
 
        /**
         * Unregister a crypter constructor.
@@ -187,12 +189,14 @@ struct crypto_factory_t {
         * Register a aead constructor.
         *
         * @param algo                  algorithm to constructor
+        * @param key size              key size to peform benchmarking for
         * @param plugin_name   plugin that registered this algorithm
         * @param create                constructor function for that algorithm
         * @return                              TRUE if registered, FALSE if test vector failed
         */
        bool (*add_aead)(crypto_factory_t *this, encryption_algorithm_t algo,
-                                        const char *plugin_name, aead_constructor_t create);
+                                        size_t key_size, const char *plugin_name,
+                                        aead_constructor_t create);
 
        /**
         * Register a signer constructor.
index 15ed173..20f64c3 100644 (file)
@@ -138,11 +138,11 @@ static u_int end_timing(struct timespec *start)
  * Benchmark a crypter
  */
 static u_int bench_crypter(private_crypto_tester_t *this,
-       encryption_algorithm_t alg, crypter_constructor_t create)
+       encryption_algorithm_t alg, crypter_constructor_t create, size_t key_size)
 {
        crypter_t *crypter;
 
-       crypter = create(alg, 0);
+       crypter = create(alg, key_size);
        if (crypter)
        {
                char iv[crypter->get_iv_size(crypter)];
@@ -280,8 +280,8 @@ failure:
        {
                if (failed)
                {
-                       DBG1(DBG_LIB,"disable %N[%s]: no key size supported",
-                                encryption_algorithm_names, alg, plugin_name);
+                       DBG1(DBG_LIB,"disable %N[%s]: %zd byte key size not supported",
+                                encryption_algorithm_names, alg, plugin_name, key_size);
                        return FALSE;
                }
                else
@@ -296,9 +296,10 @@ failure:
        {
                if (speed)
                {
-                       *speed = bench_crypter(this, alg, create);
-                       DBG1(DBG_LIB, "enabled  %N[%s]: passed %u test vectors, %d points",
-                                encryption_algorithm_names, alg, plugin_name, tested, *speed);
+                       *speed = bench_crypter(this, alg, create, key_size);
+                       DBG1(DBG_LIB, "enabled  %N[%s]: passed %u test vectors, %d points "
+                                "(%zd bit key)", encryption_algorithm_names, alg,
+                                plugin_name, tested, *speed, key_size * 8);
                }
                else
                {
@@ -313,11 +314,11 @@ failure:
  * Benchmark an aead transform
  */
 static u_int bench_aead(private_crypto_tester_t *this,
-       encryption_algorithm_t alg, aead_constructor_t create)
+       encryption_algorithm_t alg, aead_constructor_t create, size_t key_size)
 {
        aead_t *aead;
 
-       aead = create(alg, 0, 0);
+       aead = create(alg, key_size, 0);
        if (aead)
        {
                char iv[aead->get_iv_size(aead)];
@@ -474,8 +475,8 @@ failure:
        {
                if (failed)
                {
-                       DBG1(DBG_LIB,"disable %N[%s]: no key size supported",
-                                encryption_algorithm_names, alg, plugin_name);
+                       DBG1(DBG_LIB,"disable %N[%s]: %zd byte key size not supported",
+                                encryption_algorithm_names, alg, plugin_name, key_size);
                        return FALSE;
                }
                else
@@ -490,9 +491,10 @@ failure:
        {
                if (speed)
                {
-                       *speed = bench_aead(this, alg, create);
-                       DBG1(DBG_LIB, "enabled  %N[%s]: passed %u test vectors, %d points",
-                                encryption_algorithm_names, alg, plugin_name, tested, *speed);
+                       *speed = bench_aead(this, alg, create, key_size);
+                       DBG1(DBG_LIB, "enabled  %N[%s]: passed %u test vectors, %d points "
+                                "(%zd bit key)", encryption_algorithm_names, alg,
+                                plugin_name, tested, *speed, key_size * 8);
                }
                else
                {
index 65cdbe9..2d0ce8a 100644 (file)
@@ -437,10 +437,12 @@ bool plugin_feature_load(plugin_t *plugin, plugin_feature_t *feature,
        {
                case FEATURE_CRYPTER:
                        lib->crypto->add_crypter(lib->crypto, feature->arg.crypter.alg,
+                                                               feature->arg.crypter.key_size,
                                                                name, reg->arg.reg.f);
                        break;
                case FEATURE_AEAD:
                        lib->crypto->add_aead(lib->crypto, feature->arg.aead.alg,
+                                                               feature->arg.aead.key_size,
                                                                name, reg->arg.reg.f);
                        break;
                case FEATURE_SIGNER: