summaryrefslogtreecommitdiff
path: root/src/libstrongswan/crypto/hashers
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstrongswan/crypto/hashers')
-rw-r--r--src/libstrongswan/crypto/hashers/hasher.c79
-rw-r--r--src/libstrongswan/crypto/hashers/hasher.h44
2 files changed, 113 insertions, 10 deletions
diff --git a/src/libstrongswan/crypto/hashers/hasher.c b/src/libstrongswan/crypto/hashers/hasher.c
index 7fa6346d6..14bfb022f 100644
--- a/src/libstrongswan/crypto/hashers/hasher.c
+++ b/src/libstrongswan/crypto/hashers/hasher.c
@@ -19,17 +19,21 @@
* 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.
+ *
+ * RCSID $Id: hasher.c 3304 2007-10-12 23:18:42Z andreas $
*/
#include "hasher.h"
+#include <asn1/oid.h>
#include <crypto/hashers/sha1_hasher.h>
#include <crypto/hashers/sha2_hasher.h>
#include <crypto/hashers/md5_hasher.h>
-ENUM(hash_algorithm_names, HASH_MD2, HASH_SHA512,
+ENUM(hash_algorithm_names, HASH_UNKNOWN, HASH_SHA512,
+ "HASH_UNKNOWN",
"HASH_MD2",
"HASH_MD5",
"HASH_SHA1",
@@ -63,3 +67,76 @@ hasher_t *hasher_create(hash_algorithm_t hash_algorithm)
return NULL;
}
}
+
+/*
+ * Described in header.
+ */
+hash_algorithm_t hasher_algorithm_from_oid(int oid)
+{
+ hash_algorithm_t algorithm;
+
+ switch (oid)
+ {
+ case OID_MD2:
+ case OID_MD2_WITH_RSA:
+ algorithm = HASH_MD2;
+ break;
+ case OID_MD5:
+ case OID_MD5_WITH_RSA:
+ algorithm = HASH_MD5;
+ break;
+ case OID_SHA1:
+ case OID_SHA1_WITH_RSA:
+ algorithm = HASH_SHA1;
+ break;
+ case OID_SHA256:
+ case OID_SHA256_WITH_RSA:
+ algorithm = HASH_SHA256;
+ break;
+ case OID_SHA384:
+ case OID_SHA384_WITH_RSA:
+ algorithm = HASH_SHA384;
+ break;
+ case OID_SHA512:
+ case OID_SHA512_WITH_RSA:
+ algorithm = HASH_SHA512;
+ break;
+ default:
+ algorithm = HASH_UNKNOWN;
+ }
+ return algorithm;
+}
+
+/*
+ * Described in header.
+ */
+int hasher_signature_algorithm_to_oid(hash_algorithm_t alg)
+{
+ int oid;
+
+ switch (alg)
+ {
+ case HASH_MD2:
+ oid = OID_MD2_WITH_RSA;
+ break;
+ case HASH_MD5:
+ oid = OID_MD5_WITH_RSA;
+ break;
+ case HASH_SHA1:
+ oid = OID_SHA1_WITH_RSA;
+ break;
+ case HASH_SHA256:
+ oid = OID_SHA256_WITH_RSA;
+ break;
+ case HASH_SHA384:
+ oid = OID_SHA384_WITH_RSA;
+ break;
+ case HASH_SHA512:
+ oid = OID_SHA512_WITH_RSA;
+ break;
+ default:
+ oid = OID_UNKNOWN;
+ }
+ return oid;
+}
+
diff --git a/src/libstrongswan/crypto/hashers/hasher.h b/src/libstrongswan/crypto/hashers/hasher.h
index 6c17f892d..48b904576 100644
--- a/src/libstrongswan/crypto/hashers/hasher.h
+++ b/src/libstrongswan/crypto/hashers/hasher.h
@@ -19,6 +19,8 @@
* 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.
+ *
+ * RCSID $Id: hasher.h 3307 2007-10-17 02:56:24Z andreas $
*/
#ifndef HASHER_H_
@@ -42,17 +44,18 @@ typedef struct hasher_t hasher_t;
* @ingroup hashers
*/
enum hash_algorithm_t {
- HASH_MD2 = 0,
+ HASH_UNKNOWN = 0,
+ HASH_MD2 = 1,
/** Implemented in class md5_hasher_t */
- HASH_MD5 = 1,
+ HASH_MD5 = 2,
/** Implemented in class sha1_hasher_t */
- HASH_SHA1 = 2,
+ HASH_SHA1 = 3,
/** Implemented in class sha2_hasher_t */
- HASH_SHA256 = 3,
+ HASH_SHA256 = 4,
/** Implemented in class sha2_hasher_t */
- HASH_SHA384 = 4,
+ HASH_SHA384 = 5,
/** Implemented in class sha2_hasher_t */
- HASH_SHA512 = 5,
+ HASH_SHA512 = 6,
};
#define HASH_SIZE_MD2 16
@@ -68,7 +71,6 @@ enum hash_algorithm_t {
*/
extern enum_name_t *hash_algorithm_names;
-
/**
* @brief Generic interface for all hash functions.
*
@@ -82,7 +84,7 @@ struct hasher_t {
* @brief Hash data and write it in the buffer.
*
* If the parameter hash is NULL, no result is written back
- * an more data can be appended to already hashed data.
+ * and more data can be appended to already hashed data.
* If not, the result is written back and the hasher is reset.
*
* The hash output parameter must hold at least
@@ -98,7 +100,7 @@ struct hasher_t {
* @brief Hash data and allocate space for the hash.
*
* If the parameter hash is NULL, no result is written back
- * an more data can be appended to already hashed data.
+ * and more data can be appended to already hashed data.
* If not, the result is written back and the hasher is reset.
*
* @param this calling object
@@ -156,4 +158,28 @@ struct hasher_t {
*/
hasher_t *hasher_create(hash_algorithm_t hash_algorithm);
+/**
+ * @brief Conversion of ASN.1 OID to hash algorithm.
+ *
+ * @param oid ASN.1 OID
+ * @return
+ * - hash algorithm
+ * - HASH_UNKNOWN if OID unsuported
+ *
+ * @ingroup hashers
+ */
+hash_algorithm_t hasher_algorithm_from_oid(int oid);
+
+/**
+ * @brief Conversion of hash signature algorithm ASN.1 OID.
+ *
+ * @param alg hash algorithm
+ * @return
+ * - ASN.1 OID if known hash algorithm
+ * - OID_UNKNOW
+ *
+ * @ingroup hashers
+ */
+int hasher_signature_algorithm_to_oid(hash_algorithm_t alg);
+
#endif /* HASHER_H_ */