aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Willi <martin@strongswan.org>2009-06-04 15:51:20 +0200
committerMartin Willi <martin@strongswan.org>2009-06-09 11:18:56 +0200
commitf908ff9f910dee0ae727a9406025d603d63bad71 (patch)
tree615f61141447980bf7bafbf1cb8b2922337079f1
parent513a1a2835d94a7b409b40051628c7a2d1107af3 (diff)
downloadstrongswan-f908ff9f910dee0ae727a9406025d603d63bad71.tar.bz2
strongswan-f908ff9f910dee0ae727a9406025d603d63bad71.tar.xz
gcrypt hasher implementation
-rw-r--r--src/libstrongswan/plugins/gcrypt/Makefile.am3
-rw-r--r--src/libstrongswan/plugins/gcrypt/gcrypt_hasher.c151
-rw-r--r--src/libstrongswan/plugins/gcrypt/gcrypt_hasher.h47
-rw-r--r--src/libstrongswan/plugins/gcrypt/gcrypt_plugin.c20
4 files changed, 220 insertions, 1 deletions
diff --git a/src/libstrongswan/plugins/gcrypt/Makefile.am b/src/libstrongswan/plugins/gcrypt/Makefile.am
index 237df30e3..94edcc59f 100644
--- a/src/libstrongswan/plugins/gcrypt/Makefile.am
+++ b/src/libstrongswan/plugins/gcrypt/Makefile.am
@@ -5,7 +5,8 @@ AM_CFLAGS = -rdynamic
plugin_LTLIBRARIES = libstrongswan-gcrypt.la
-libstrongswan_gcrypt_la_SOURCES = gcrypt_plugin.h gcrypt_plugin.c
+libstrongswan_gcrypt_la_SOURCES = gcrypt_plugin.h gcrypt_plugin.c \
+ gcrypt_hasher.h gcrypt_hasher.c
libstrongswan_gcrypt_la_LDFLAGS = -module
libstrongswan_gcrypt_la_LIBADD = -lgcrypt
diff --git a/src/libstrongswan/plugins/gcrypt/gcrypt_hasher.c b/src/libstrongswan/plugins/gcrypt/gcrypt_hasher.c
new file mode 100644
index 000000000..785ebda90
--- /dev/null
+++ b/src/libstrongswan/plugins/gcrypt/gcrypt_hasher.c
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2009 Martin Willi
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include "gcrypt_hasher.h"
+
+#include <debug.h>
+
+#include <gcrypt.h>
+
+typedef struct private_gcrypt_hasher_t private_gcrypt_hasher_t;
+
+/**
+ * Private data of gcrypt_hasher_t
+ */
+struct private_gcrypt_hasher_t {
+
+ /**
+ * Public part of this class.
+ */
+ gcrypt_hasher_t public;
+
+ /**
+ * gcrypt hasher context
+ */
+ gcry_md_hd_t hd;
+};
+
+/**
+ * Implementation of hasher_t.get_hash_size.
+ */
+static size_t get_hash_size(private_gcrypt_hasher_t *this)
+{
+ return gcry_md_get_algo_dlen(gcry_md_get_algo(this->hd));
+}
+
+/**
+ * Implementation of hasher_t.reset.
+ */
+static void reset(private_gcrypt_hasher_t *this)
+{
+ gcry_md_reset(this->hd);
+}
+
+/**
+ * Implementation of hasher_t.get_hash.
+ */
+static void get_hash(private_gcrypt_hasher_t *this, chunk_t chunk,
+ u_int8_t *hash)
+{
+ gcry_md_write(this->hd, chunk.ptr, chunk.len);
+ if (hash)
+ {
+ memcpy(hash, gcry_md_read(this->hd, 0), get_hash_size(this));
+ gcry_md_reset(this->hd);
+ }
+}
+
+/**
+ * Implementation of hasher_t.allocate_hash.
+ */
+static void allocate_hash(private_gcrypt_hasher_t *this, chunk_t chunk,
+ chunk_t *hash)
+{
+ if (hash)
+ {
+ *hash = chunk_alloc(get_hash_size(this));
+ get_hash(this, chunk, hash->ptr);
+ }
+ else
+ {
+ get_hash(this, chunk, NULL);
+ }
+}
+
+/**
+ * Implementation of hasher_t.destroy.
+ */
+static void destroy (private_gcrypt_hasher_t *this)
+{
+ gcry_md_close(this->hd);
+ free(this);
+}
+
+/*
+ * Described in header
+ */
+gcrypt_hasher_t *gcrypt_hasher_create(hash_algorithm_t algo)
+{
+ private_gcrypt_hasher_t *this;
+ int gcrypt_alg;
+ gcry_error_t err;
+
+ switch (algo)
+ {
+ case HASH_MD2:
+ gcrypt_alg = GCRY_MD_MD2;
+ break;
+ case HASH_MD4:
+ gcrypt_alg = GCRY_MD_MD4;
+ break;
+ case HASH_MD5:
+ gcrypt_alg = GCRY_MD_MD5;
+ break;
+ case HASH_SHA1:
+ gcrypt_alg = GCRY_MD_SHA1;
+ break;
+ case HASH_SHA256:
+ gcrypt_alg = GCRY_MD_SHA256;
+ break;
+ case HASH_SHA384:
+ gcrypt_alg = GCRY_MD_SHA384;
+ break;
+ case HASH_SHA512:
+ gcrypt_alg = GCRY_MD_SHA512;
+ break;
+ default:
+ return NULL;
+ }
+
+ this = malloc_thing(private_gcrypt_hasher_t);
+
+ err = gcry_md_open(&this->hd, gcrypt_alg, 0);
+ if (err)
+ {
+ DBG1("grcy_md_open(%N) failed: %s",
+ hash_algorithm_names, algo, gpg_strerror(err));
+ free(this);
+ return NULL;
+ }
+
+ this->public.hasher_interface.get_hash = (void (*) (hasher_t*, chunk_t, u_int8_t*))get_hash;
+ this->public.hasher_interface.allocate_hash = (void (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
+ this->public.hasher_interface.get_hash_size = (size_t (*) (hasher_t*))get_hash_size;
+ this->public.hasher_interface.reset = (void (*) (hasher_t*))reset;
+ this->public.hasher_interface.destroy = (void (*) (hasher_t*))destroy;
+
+ return &this->public;
+}
+
diff --git a/src/libstrongswan/plugins/gcrypt/gcrypt_hasher.h b/src/libstrongswan/plugins/gcrypt/gcrypt_hasher.h
new file mode 100644
index 000000000..6f724fba8
--- /dev/null
+++ b/src/libstrongswan/plugins/gcrypt/gcrypt_hasher.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2009 Martin Willi
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup gcrypt_hasher gcrypt_hasher
+ * @{ @ingroup gcrypt_p
+ */
+
+#ifndef GCRYPT_HASHER_H_
+#define GCRYPT_HASHER_H_
+
+typedef struct gcrypt_hasher_t gcrypt_hasher_t;
+
+#include <crypto/hashers/hasher.h>
+
+/**
+ * Implementation of hashers using libgcrypt.
+ */
+struct gcrypt_hasher_t {
+
+ /**
+ * The hasher_t interface.
+ */
+ hasher_t hasher_interface;
+};
+
+/**
+ * Constructor to create gcrypt_hasher_t.
+ *
+ * @param algo algorithm
+ * @return gcrypt_hasher_t, NULL if not supported
+ */
+gcrypt_hasher_t *gcrypt_hasher_create(hash_algorithm_t algo);
+
+#endif /** GCRYPT_HASHER_H_ @}*/
diff --git a/src/libstrongswan/plugins/gcrypt/gcrypt_plugin.c b/src/libstrongswan/plugins/gcrypt/gcrypt_plugin.c
index c31dbe33d..062e890c8 100644
--- a/src/libstrongswan/plugins/gcrypt/gcrypt_plugin.c
+++ b/src/libstrongswan/plugins/gcrypt/gcrypt_plugin.c
@@ -15,6 +15,8 @@
#include "gcrypt_plugin.h"
+#include "gcrypt_hasher.h"
+
#include <library.h>
#include <debug.h>
@@ -45,6 +47,8 @@ GCRY_THREAD_OPTION_PTHREAD_IMPL;
*/
static void destroy(private_gcrypt_plugin_t *this)
{
+ lib->crypto->remove_hasher(lib->crypto,
+ (hasher_constructor_t)gcrypt_hasher_create);
free(this);
}
@@ -71,6 +75,22 @@ 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);
+
return &this->public.plugin;
}