]> git.ipfire.org Git - people/ms/strongswan.git/commitdiff
Function added to convert a hash algorithm to an HMAC integrity algorithm
authorTobias Brunner <tobias@strongswan.org>
Thu, 11 Apr 2013 17:41:48 +0000 (19:41 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 8 May 2013 13:02:39 +0000 (15:02 +0200)
src/libstrongswan/crypto/hashers/hasher.c
src/libstrongswan/crypto/hashers/hasher.h

index dc73d5223c26a9276340a010370808aa7556e6b4..4ed48ba364cee269f3692278c947be2f8d5d4103 100644 (file)
@@ -177,6 +177,72 @@ hash_algorithm_t hasher_algorithm_from_integrity(integrity_algorithm_t alg,
        return HASH_UNKNOWN;
 }
 
+/*
+ * Described in header.
+ */
+integrity_algorithm_t hasher_algorithm_to_integrity(hash_algorithm_t alg,
+                                                                                                       size_t length)
+{
+       switch (alg)
+       {
+               case HASH_MD5:
+                       switch (length)
+                       {
+                               case 12:
+                                       return AUTH_HMAC_MD5_96;
+                               case 16:
+                                       return AUTH_HMAC_MD5_128;
+                       }
+                       break;
+               case HASH_SHA1:
+               case HASH_PREFERRED:
+                       switch (length)
+                       {
+                               case 12:
+                                       return AUTH_HMAC_SHA1_96;
+                               case 16:
+                                       return AUTH_HMAC_SHA1_128;
+                               case 20:
+                                       return AUTH_HMAC_SHA1_160;
+                       }
+                       break;
+               case HASH_SHA256:
+                       switch (length)
+                       {
+                               case 12:
+                                       return AUTH_HMAC_SHA2_256_96;
+                               case 16:
+                                       return AUTH_HMAC_SHA2_256_128;
+                               case 32:
+                                       return AUTH_HMAC_SHA2_256_256;
+                       }
+                       break;
+               case HASH_SHA384:
+                       switch (length)
+                       {
+                               case 24:
+                                       return AUTH_HMAC_SHA2_384_192;
+                               case 48:
+                                       return AUTH_HMAC_SHA2_384_384;
+
+                       }
+                       break;
+               case HASH_SHA512:
+                       switch (length)
+                       {
+                               case 32:
+                                       return AUTH_HMAC_SHA2_512_256;
+                       }
+                       break;
+               case HASH_MD2:
+               case HASH_MD4:
+               case HASH_SHA224:
+               case HASH_UNKNOWN:
+                       break;
+       }
+       return AUTH_UNDEFINED;
+}
+
 /*
  * Described in header.
  */
index 759f6a23c86c7002e7f96ccbe00a056acadb9b6b..4e46fca104ebf72a970edbbd904cd48bd56566a9 100644 (file)
@@ -153,6 +153,17 @@ hash_algorithm_t hasher_algorithm_from_prf(pseudo_random_function_t alg);
 hash_algorithm_t hasher_algorithm_from_integrity(integrity_algorithm_t alg,
                                                                                                 size_t *length);
 
+/**
+ * Conversion of hash algorithm to integrity algorithm (if based on a hash).
+ *
+ * @param alg                  hash algorithm
+ * @param length               length of the signature
+ * @return                             integrity algorithm, AUTH_UNDEFINED if none is known
+ *                                             based on the given hash function
+ */
+integrity_algorithm_t hasher_algorithm_to_integrity(hash_algorithm_t alg,
+                                                                                                       size_t length);
+
 /**
  * Conversion of hash algorithm into ASN.1 OID.
  *