hasher: Add function to determine length of hashes
authorTobias Brunner <tobias@strongswan.org>
Fri, 22 Sep 2017 07:46:14 +0000 (09:46 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 8 Nov 2017 15:48:10 +0000 (16:48 +0100)
src/libstrongswan/crypto/hashers/hasher.c
src/libstrongswan/crypto/hashers/hasher.h

index 3f49f36..c04835b 100644 (file)
@@ -57,6 +57,44 @@ ENUM_NEXT(hash_algorithm_short_names, HASH_UNKNOWN, HASH_SHA3_512, HASH_IDENTITY
 ENUM_END(hash_algorithm_short_names, HASH_SHA3_512);
 
 /*
+ * Described in header
+ */
+size_t hasher_hash_size(hash_algorithm_t alg)
+{
+       switch (alg)
+       {
+               case HASH_SHA1:
+                       return HASH_SIZE_SHA1;
+               case HASH_SHA256:
+                       return HASH_SIZE_SHA256;
+               case HASH_SHA384:
+                       return HASH_SIZE_SHA384;
+               case HASH_SHA512:
+                       return HASH_SIZE_SHA512;
+               case HASH_MD2:
+                       return HASH_SIZE_MD2;
+               case HASH_MD4:
+                       return HASH_SIZE_MD4;
+               case HASH_MD5:
+                       return HASH_SIZE_MD5;
+               case HASH_SHA224:
+                       return HASH_SIZE_SHA224;
+               case HASH_SHA3_224:
+                       return HASH_SIZE_SHA224;
+               case HASH_SHA3_256:
+                       return HASH_SIZE_SHA256;
+               case HASH_SHA3_384:
+                       return HASH_SIZE_SHA384;
+               case HASH_SHA3_512:
+                       return HASH_SIZE_SHA512;
+               case HASH_IDENTITY:
+               case HASH_UNKNOWN:
+                       break;
+       }
+       return 0;
+}
+
+/*
  * Described in header.
  */
 hash_algorithm_t hasher_algorithm_from_oid(int oid)
index ec0c632..9d2df1d 100644 (file)
@@ -131,6 +131,14 @@ struct hasher_t {
 };
 
 /**
+ * Returns the size of the hash for the given algorithm.
+ *
+ * @param alg                  hash algorithm
+ * @return                             size of hash or 0 if unknown
+ */
+size_t hasher_hash_size(hash_algorithm_t alg);
+
+/**
  * Conversion of ASN.1 OID to hash algorithm.
  *
  * @param oid                  ASN.1 OID