use abstract mutex_t for gcrypt locking callbacks
[strongswan.git] / src / libstrongswan / plugins / gcrypt / gcrypt_plugin.c
index c31dbe3..4f66e34 100644 (file)
 
 #include "gcrypt_plugin.h"
 
+#include "gcrypt_hasher.h"
+#include "gcrypt_crypter.h"
+
 #include <library.h>
 #include <debug.h>
+#include <utils/mutex.h>
 
 #include <errno.h>
 #include <gcrypt.h>
-#include <pthread.h>
 
 typedef struct private_gcrypt_plugin_t private_gcrypt_plugin_t;
 
@@ -36,15 +39,65 @@ struct private_gcrypt_plugin_t {
 };
 
 /**
- * Thread callback implementations for pthread
+ * gcrypt mutex initialization wrapper
  */
-GCRY_THREAD_OPTION_PTHREAD_IMPL;
+static int mutex_init(void **lock)
+{
+       *lock = mutex_create(MUTEX_DEFAULT);
+       return 0;
+}
+
+/**
+ * gcrypt mutex cleanup wrapper
+ */
+static int mutex_destroy(void **lock)
+{
+       mutex_t *mutex = *lock;
+       
+       mutex->destroy(mutex);
+       return 0;
+}
+
+/**
+ * gcrypt mutex lock wrapper
+ */
+static int mutex_lock(void **lock)
+{
+       mutex_t *mutex = *lock;
+       
+       mutex->lock(mutex);
+       return 0;
+}
+
+/**
+ * gcrypt mutex unlock wrapper
+ */
+static int mutex_unlock(void **lock)
+{
+       mutex_t *mutex = *lock;
+       
+       mutex->unlock(mutex);
+       return 0;
+}
+
+/**
+ * gcrypt locking functions using our mutex_t
+ */
+static struct gcry_thread_cbs thread_functions = {
+       GCRY_THREAD_OPTION_USER, NULL,
+       mutex_init, mutex_destroy, mutex_lock, mutex_unlock,
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+};
 
 /**
  * Implementation of gcrypt_plugin_t.destroy
  */
 static void destroy(private_gcrypt_plugin_t *this)
 {
+       lib->crypto->remove_hasher(lib->crypto,
+                                       (hasher_constructor_t)gcrypt_hasher_create);
+       lib->crypto->remove_crypter(lib->crypto,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
        free(this);
 }
 
@@ -55,7 +108,7 @@ plugin_t *plugin_create()
 {
        private_gcrypt_plugin_t *this;
        
-       gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
+       gcry_control(GCRYCTL_SET_THREAD_CBS, &thread_functions);
        
        if (!gcry_check_version(GCRYPT_VERSION))
        {
@@ -71,6 +124,44 @@ plugin_t *plugin_create()
        
        this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
        
+       /* hashers */
+       lib->crypto->add_hasher(lib->crypto, HASH_SHA1,
+                                       (hasher_constructor_t)gcrypt_hasher_create);
+       lib->crypto->add_hasher(lib->crypto, HASH_MD2,
+                                       (hasher_constructor_t)gcrypt_hasher_create);
+       lib->crypto->add_hasher(lib->crypto, HASH_MD4,
+                                       (hasher_constructor_t)gcrypt_hasher_create);
+       lib->crypto->add_hasher(lib->crypto, HASH_MD5,
+                                       (hasher_constructor_t)gcrypt_hasher_create);
+       lib->crypto->add_hasher(lib->crypto, HASH_SHA256,
+                                       (hasher_constructor_t)gcrypt_hasher_create);
+       lib->crypto->add_hasher(lib->crypto, HASH_SHA384,
+                                       (hasher_constructor_t)gcrypt_hasher_create);
+       lib->crypto->add_hasher(lib->crypto, HASH_SHA512,
+                                       (hasher_constructor_t)gcrypt_hasher_create);
+       
+       /* crypters */
+       lib->crypto->add_crypter(lib->crypto, ENCR_3DES,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       lib->crypto->add_crypter(lib->crypto, ENCR_IDEA,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       lib->crypto->add_crypter(lib->crypto, ENCR_CAST,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       lib->crypto->add_crypter(lib->crypto, ENCR_BLOWFISH,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       lib->crypto->add_crypter(lib->crypto, ENCR_DES,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       lib->crypto->add_crypter(lib->crypto, ENCR_DES_ECB,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       lib->crypto->add_crypter(lib->crypto, ENCR_AES_CBC,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       lib->crypto->add_crypter(lib->crypto, ENCR_CAMELLIA_CBC,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       lib->crypto->add_crypter(lib->crypto, ENCR_SERPENT_CBC,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       lib->crypto->add_crypter(lib->crypto, ENCR_TWOFISH_CBC,
+                                       (crypter_constructor_t)gcrypt_crypter_create);
+       
        return &this->public.plugin;
 }