]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
hash-algorithm-set: Add class to manage a set of hash algorithms
authorTobias Brunner <tobias@strongswan.org>
Tue, 3 Mar 2015 17:03:28 +0000 (18:03 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 4 Mar 2015 12:54:11 +0000 (13:54 +0100)
src/libstrongswan/Android.mk
src/libstrongswan/Makefile.am
src/libstrongswan/crypto/hashers/hash_algorithm_set.c [new file with mode: 0644]
src/libstrongswan/crypto/hashers/hash_algorithm_set.h [new file with mode: 0644]

index 9b775f9b3a1b9e4adb331da87594cf557b7b5baa..9b6f3f03cba2d79da43e9fb1ea80f2bc63f9e2e7 100644 (file)
@@ -8,6 +8,7 @@ asn1/asn1.c asn1/asn1_parser.c asn1/oid.c bio/bio_reader.c bio/bio_writer.c \
 collections/blocking_queue.c collections/enumerator.c collections/hashtable.c \
 collections/array.c \
 collections/linked_list.c crypto/crypters/crypter.c crypto/hashers/hasher.c \
+crypto/hashers/hash_algorithm_set.c \
 crypto/proposal/proposal_keywords.c crypto/proposal/proposal_keywords_static.c \
 crypto/prfs/prf.c crypto/prfs/mac_prf.c crypto/pkcs5.c \
 crypto/rngs/rng.c crypto/prf_plus.c crypto/signers/signer.c \
index 91d92c3f3e3508227b1aa71b452a8690412f91d3..47477de12a196b6b06fe85e52665eceb530eab44 100644 (file)
@@ -6,6 +6,7 @@ asn1/asn1.c asn1/asn1_parser.c asn1/oid.c bio/bio_reader.c bio/bio_writer.c \
 collections/blocking_queue.c collections/enumerator.c collections/hashtable.c \
 collections/array.c \
 collections/linked_list.c crypto/crypters/crypter.c crypto/hashers/hasher.c \
+crypto/hashers/hash_algorithm_set.c \
 crypto/proposal/proposal_keywords.c crypto/proposal/proposal_keywords_static.c \
 crypto/prfs/prf.c crypto/prfs/mac_prf.c crypto/pkcs5.c \
 crypto/rngs/rng.c crypto/prf_plus.c crypto/signers/signer.c \
@@ -61,7 +62,8 @@ library.h \
 asn1/asn1.h asn1/asn1_parser.h asn1/oid.h bio/bio_reader.h bio/bio_writer.h \
 collections/blocking_queue.h collections/enumerator.h collections/hashtable.h \
 collections/linked_list.h collections/array.h collections/dictionary.h \
-crypto/crypters/crypter.h crypto/hashers/hasher.h crypto/mac.h \
+crypto/crypters/crypter.h crypto/hashers/hasher.h \
+crypto/hashers/hash_algorithm_set.h crypto/mac.h \
 crypto/proposal/proposal_keywords.h crypto/proposal/proposal_keywords_static.h \
 crypto/prfs/prf.h crypto/prfs/mac_prf.h crypto/rngs/rng.h crypto/nonce_gen.h \
 crypto/prf_plus.h crypto/signers/signer.h crypto/signers/mac_signer.h \
diff --git a/src/libstrongswan/crypto/hashers/hash_algorithm_set.c b/src/libstrongswan/crypto/hashers/hash_algorithm_set.c
new file mode 100644 (file)
index 0000000..93b67cb
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2015 Tobias Brunner
+ * 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 "hash_algorithm_set.h"
+
+#include <collections/array.h>
+
+typedef struct private_hash_algorithm_set_t private_hash_algorithm_set_t;
+
+struct private_hash_algorithm_set_t {
+
+       /**
+        * Public interface
+        */
+       hash_algorithm_set_t public;
+
+       /**
+        * Algorithms contained in the set
+        */
+       array_t *algorithms;
+};
+
+/**
+ * Sort hash algorithms
+ */
+static int hash_sort(const void *a, const void *b, void *user)
+{
+       const hash_algorithm_t *ha = a, *hb = b;
+       return *ha - *hb;
+}
+
+/**
+ * Find a hash algorithm
+ */
+static int hash_find(const void *a, const void *b)
+{
+       return hash_sort(a, b, NULL);
+}
+
+METHOD(hash_algorithm_set_t, contains, bool,
+       private_hash_algorithm_set_t *this, hash_algorithm_t hash)
+{
+       return array_bsearch(this->algorithms, &hash, hash_find, NULL) != -1;
+}
+
+METHOD(hash_algorithm_set_t, add, void,
+       private_hash_algorithm_set_t *this, hash_algorithm_t hash)
+{
+       if (!contains(this, hash))
+       {
+               array_insert(this->algorithms, ARRAY_TAIL, &hash);
+               array_sort(this->algorithms, hash_sort, NULL);
+       }
+}
+
+METHOD(hash_algorithm_set_t, count, int,
+       private_hash_algorithm_set_t *this)
+{
+       return array_count(this->algorithms);
+}
+
+static bool hash_filter(void *data, void **in, hash_algorithm_t *out)
+{
+       *out = **(hash_algorithm_t**)in;
+       return TRUE;
+}
+
+METHOD(hash_algorithm_set_t, create_enumerator, enumerator_t*,
+       private_hash_algorithm_set_t *this)
+{
+       return enumerator_create_filter(array_create_enumerator(this->algorithms),
+                                                                       (void*)hash_filter, NULL, NULL);
+}
+
+METHOD(hash_algorithm_set_t, destroy, void,
+       private_hash_algorithm_set_t *this)
+{
+       array_destroy(this->algorithms);
+       free(this);
+}
+
+/**
+ * Described in header
+ */
+hash_algorithm_set_t *hash_algorithm_set_create()
+{
+       private_hash_algorithm_set_t *this;
+
+       INIT(this,
+               .public = {
+                       .add = _add,
+                       .contains = _contains,
+                       .count = _count,
+                       .create_enumerator = _create_enumerator,
+                       .destroy = _destroy,
+               },
+               .algorithms = array_create(sizeof(hash_algorithm_t), 0),
+       );
+
+       return &this->public;
+}
\ No newline at end of file
diff --git a/src/libstrongswan/crypto/hashers/hash_algorithm_set.h b/src/libstrongswan/crypto/hashers/hash_algorithm_set.h
new file mode 100644 (file)
index 0000000..00e90cc
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2015 Tobias Brunner
+ * 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 hash_algorithm_set hash_algorithm_set
+ * @{ @ingroup crypto
+ */
+
+#ifndef HASH_ALGORITHM_SET_H_
+#define HASH_ALGORITHM_SET_H_
+
+typedef struct hash_algorithm_set_t hash_algorithm_set_t;
+
+#include <library.h>
+#include <crypto/hashers/hasher.h>
+
+/**
+ * A set of hash algorithms
+ */
+struct hash_algorithm_set_t {
+
+       /**
+        * Add the given algorithm to the set.
+        *
+        * @param alg           hash algorithm
+        */
+       void (*add)(hash_algorithm_set_t *this, hash_algorithm_t alg);
+
+       /**
+        * Check if the given algorithm is contained in the set.
+        *
+        * @param alg           hash algorithm
+        * @return                      TRUE if contained in set
+        */
+       bool (*contains)(hash_algorithm_set_t *this, hash_algorithm_t alg);
+
+       /**
+        * Number of hash algorithms contained in the set.
+        *
+        * @return                      number of algorithms
+        */
+       int (*count)(hash_algorithm_set_t *this);
+
+       /**
+        * Enumerate the algorithms contained in the set.
+        *
+        * @return                      enumerator over hash_algorithm_t (sorted by identifier)
+        */
+       enumerator_t *(*create_enumerator)(hash_algorithm_set_t *this);
+
+       /**
+        * Destroy a hash_algorithm_set_t instance
+        */
+       void (*destroy)(hash_algorithm_set_t *this);
+};
+
+/**
+ * Create a set of hash algorithms.
+ *
+ * @return                             hash_algorithm_set_t instance
+ */
+hash_algorithm_set_t *hash_algorithm_set_create();
+
+#endif /** HASH_ALGORITHM_SET_H_ @}*/