]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
hash: enumerate hash algorithms
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 13 May 2020 10:51:41 +0000 (12:51 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 14 May 2020 13:37:38 +0000 (15:37 +0200)
Identify the algorithms with an enum instead of string.

client.c
hash.h
hash_intmd5.c
hash_nettle.c
hash_nss.c
hash_tomcrypt.c
keys.c
test/unit/hash.c
util.c
util.h

index 9e64c6d9b704e7a4f43420affd9cd4f17f70465e..f418c73f85ce0039544feb5036325fcfca2ecd9f 100644 (file)
--- a/client.c
+++ b/client.c
@@ -2990,7 +2990,7 @@ process_cmd_keygen(char *line)
   cmac_length = 0;
 #endif
 
-  if (HSH_GetHashId(type) >= 0) {
+  if (HSH_GetHashId(UTI_HashNameToAlgorithm(type)) >= 0) {
     length = (bits + 7) / 8;
   } else if (cmac_length > 0) {
     length = cmac_length;
diff --git a/hash.h b/hash.h
index 185da661533c1c79a5c4400fe223fa2003e03295..12167bc002acaf0a2ffb732ea5eb84511c5c87eb 100644 (file)
--- a/hash.h
+++ b/hash.h
 /* length of hash values produced by SHA512 */
 #define MAX_HASH_LENGTH 64
 
-extern int HSH_GetHashId(const char *name);
+typedef enum {
+  HSH_INVALID = 0,
+  HSH_MD5 = 1,
+  HSH_SHA1 = 2,
+  HSH_SHA256 = 3,
+  HSH_SHA384 = 4,
+  HSH_SHA512 = 5,
+  HSH_SHA3_224 = 6,
+  HSH_SHA3_256 = 7,
+  HSH_SHA3_384 = 8,
+  HSH_SHA3_512 = 9,
+  HSH_TIGER = 10,
+  HSH_WHIRLPOOL = 11,
+} HSH_Algorithm;
+
+extern int HSH_GetHashId(HSH_Algorithm algorithm);
 
 extern unsigned int HSH_Hash(int id,
     const unsigned char *in1, unsigned int in1_len,
index 49da1cf085576cca2488b23b5a2fd1fae089c8e8..5a0debfe66feff1265c06a6772c0bfc8d0b700bf 100644 (file)
 static MD5_CTX ctx;
 
 int
-HSH_GetHashId(const char *name)
+HSH_GetHashId(HSH_Algorithm algorithm)
 {
   /* only MD5 is supported */
-  if (strcmp(name, "MD5"))
+  if (algorithm != HSH_MD5)
     return -1;
 
   return 0;
index 2622f76977262d328bb5cc734f07484791421f24..66fbe755a4deeb38270eabdb9c24d6d401e460fc 100644 (file)
 #include "memory.h"
 
 struct hash {
-  const char *name;
+  const HSH_Algorithm algorithm;
   const char *int_name;
   const struct nettle_hash *nettle_hash;
   void *context;
 };
 
 static struct hash hashes[] = {
-  { "MD5", "md5", NULL, NULL },
-  { "SHA1", "sha1", NULL, NULL },
-  { "SHA256", "sha256", NULL, NULL },
-  { "SHA384", "sha384", NULL, NULL },
-  { "SHA512", "sha512", NULL, NULL },
-  { "SHA3-224", "sha3_224", NULL, NULL },
-  { "SHA3-256", "sha3_256", NULL, NULL },
-  { "SHA3-384", "sha3_384", NULL, NULL },
-  { "SHA3-512", "sha3_512", NULL, NULL },
-  { NULL, NULL, NULL, NULL }
+  { HSH_MD5, "md5", NULL, NULL },
+  { HSH_SHA1, "sha1", NULL, NULL },
+  { HSH_SHA256, "sha256", NULL, NULL },
+  { HSH_SHA384, "sha384", NULL, NULL },
+  { HSH_SHA512, "sha512", NULL, NULL },
+  { HSH_SHA3_224, "sha3_224", NULL, NULL },
+  { HSH_SHA3_256, "sha3_256", NULL, NULL },
+  { HSH_SHA3_384, "sha3_384", NULL, NULL },
+  { HSH_SHA3_512, "sha3_512", NULL, NULL },
+  { 0, NULL, NULL, NULL }
 };
 
 int
-HSH_GetHashId(const char *name)
+HSH_GetHashId(HSH_Algorithm algorithm)
 {
   int id, nid;
 
-  for (id = 0; hashes[id].name; id++) {
-    if (!strcmp(name, hashes[id].name))
+  for (id = 0; hashes[id].algorithm != 0; id++) {
+    if (hashes[id].algorithm == algorithm)
       break;
   }
 
-  if (!hashes[id].name)
+  if (hashes[id].algorithm == 0)
     return -1;
 
   if (hashes[id].context)
@@ -112,7 +112,7 @@ HSH_Finalise(void)
 {
   int i;
 
-  for (i = 0; hashes[i].name; i++) {
+  for (i = 0; hashes[i].algorithm != 0; i++) {
     if (hashes[i].context)
       Free(hashes[i].context);
   }
index 9967bf8ae0a023acd84d364dbf0bd3558e354dc0..81345c22d22e8218df201b4cf24dcb1890e61268 100644 (file)
@@ -38,30 +38,30 @@ static NSSLOWInitContext *ictx;
 
 struct hash {
   HASH_HashType type;
-  const char *name;
+  HSH_Algorithm algorithm;
   NSSLOWHASHContext *context;
 };
 
 static struct hash hashes[] = {
-  { HASH_AlgMD5, "MD5", NULL },
-  { HASH_AlgSHA1, "SHA1", NULL },
-  { HASH_AlgSHA256, "SHA256", NULL },
-  { HASH_AlgSHA384, "SHA384", NULL },
-  { HASH_AlgSHA512, "SHA512", NULL },
-  { 0, NULL, NULL }
+  { HASH_AlgMD5, HSH_MD5, NULL },
+  { HASH_AlgSHA1, HSH_SHA1, NULL },
+  { HASH_AlgSHA256, HSH_SHA256, NULL },
+  { HASH_AlgSHA384, HSH_SHA384, NULL },
+  { HASH_AlgSHA512, HSH_SHA512, NULL },
+  { 0, 0, NULL }
 };
 
 int
-HSH_GetHashId(const char *name)
+HSH_GetHashId(HSH_Algorithm algorithm)
 {
   int i;
 
-  for (i = 0; hashes[i].name; i++) {
-    if (!strcmp(name, hashes[i].name))
+  for (i = 0; hashes[i].algorithm != 0; i++) {
+    if (hashes[i].algorithm == algorithm)
       break;
   }
 
-  if (!hashes[i].name)
+  if (hashes[i].algorithm == 0)
     return -1; /* not found */
 
   if (!ictx && !(ictx = NSSLOW_Init()))
@@ -99,7 +99,7 @@ HSH_Finalise(void)
 {
   int i;
 
-  for (i = 0; hashes[i].name; i++) {
+  for (i = 0; hashes[i].algorithm != 0; i++) {
     if (hashes[i].context)
       NSSLOWHASH_Destroy(hashes[i].context);
   }
index 8ee04909b252f30306018927d9d0d523cee1f23a..5da09b27039c8e1632e2aba3991211fab1822c4f 100644 (file)
 #include "util.h"
 
 struct hash {
-  const char *name;
+  HSH_Algorithm algorithm;
   const char *int_name;
   const struct ltc_hash_descriptor *desc;
 };
 
 static const struct hash hashes[] = {
-  { "MD5", "md5", &md5_desc },
+  { HSH_MD5, "md5", &md5_desc },
 #ifdef LTC_SHA1
-  { "SHA1", "sha1", &sha1_desc },
+  { HSH_SHA1, "sha1", &sha1_desc },
 #endif
 #ifdef LTC_SHA256
-  { "SHA256", "sha256", &sha256_desc },
+  { HSH_SHA256, "sha256", &sha256_desc },
 #endif
 #ifdef LTC_SHA384
-  { "SHA384", "sha384", &sha384_desc },
+  { HSH_SHA384, "sha384", &sha384_desc },
 #endif
 #ifdef LTC_SHA512
-  { "SHA512", "sha512", &sha512_desc },
+  { HSH_SHA512, "sha512", &sha512_desc },
 #endif
 #ifdef LTC_SHA3
-  { "SHA3-224", "sha3-224", &sha3_224_desc },
-  { "SHA3-256", "sha3-256", &sha3_256_desc },
-  { "SHA3-384", "sha3-384", &sha3_384_desc },
-  { "SHA3-512", "sha3-512", &sha3_512_desc },
+  { HSH_SHA3_224, "sha3-224", &sha3_224_desc },
+  { HSH_SHA3_256, "sha3-256", &sha3_256_desc },
+  { HSH_SHA3_384, "sha3-384", &sha3_384_desc },
+  { HSH_SHA3_512, "sha3-512", &sha3_512_desc },
 #endif
 #ifdef LTC_TIGER
-  { "TIGER", "tiger", &tiger_desc },
+  { HSH_TIGER, "tiger", &tiger_desc },
 #endif
 #ifdef LTC_WHIRLPOOL
-  { "WHIRLPOOL", "whirlpool", &whirlpool_desc },
+  { HSH_WHIRLPOOL, "whirlpool", &whirlpool_desc },
 #endif
-  { NULL, NULL, NULL }
+  { 0, NULL, NULL }
 };
 
 int
-HSH_GetHashId(const char *name)
+HSH_GetHashId(HSH_Algorithm algorithm)
 {
   int i, h;
 
-  for (i = 0; hashes[i].name; i++) {
-    if (!strcmp(name, hashes[i].name))
+  for (i = 0; hashes[i].algorithm != 0; i++) {
+    if (hashes[i].algorithm == algorithm)
       break;
   }
 
-  if (!hashes[i].name)
+  if (hashes[i].algorithm == 0)
     return -1; /* not found */
 
   h = find_hash(hashes[i].int_name);
diff --git a/keys.c b/keys.c
index 1fa22159037c2d15cb8a242fde0f3429f53e0313..749ca381b26a794ca63c3b54872656419e078fae 100644 (file)
--- a/keys.c
+++ b/keys.c
@@ -201,6 +201,7 @@ KEY_Reload(void)
   FILE *in;
   char line[2048], *key_file, *key_value;
   const char *key_type;
+  HSH_Algorithm hash_algorithm;
   int hash_id;
   Key key;
 
@@ -238,10 +239,15 @@ KEY_Reload(void)
       continue;
     }
 
-    hash_id = HSH_GetHashId(key_type);
     cmac_key_length = CMC_GetKeyLength(key_type);
+    hash_algorithm = UTI_HashNameToAlgorithm(key_type);
 
-    if (hash_id >= 0) {
+    if (hash_algorithm != 0) {
+      hash_id = HSH_GetHashId(hash_algorithm);
+      if (hash_id < 0) {
+        LOG(LOGS_WARN, "Unsupported %s in key %"PRIu32, "hash function", key.id);
+        continue;
+      }
       key.class = NTP_MAC;
       key.data.ntp_mac.value = MallocArray(unsigned char, key_length);
       memcpy(key.data.ntp_mac.value, key_value, key_length);
index f1e44bc49eb3f141dd34629e29f613c52e89bd9a..331dcac77c79007f0d9bab2189dc69f811285db0 100644 (file)
@@ -22,6 +22,7 @@
 #include <sysincl.h>
 #include <hash.h>
 #include <logging.h>
+#include <util.h>
 #include "test.h"
 
 struct hash_test {
@@ -69,18 +70,23 @@ test_unit(void)
     { "", "", 0 }
   };
 
+  HSH_Algorithm algorithm;
   unsigned int length;
   int i, j, hash_id;
 
+  TEST_CHECK(HSH_INVALID == 0);
+
   for (i = 0; tests[i].name[0] != '\0'; i++) {
-    hash_id = HSH_GetHashId(tests[i].name);
+    algorithm = UTI_HashNameToAlgorithm(tests[i].name);
+    TEST_CHECK(algorithm != 0);
+    hash_id = HSH_GetHashId(algorithm);
     if (hash_id < 0) {
-      TEST_CHECK(strcmp(tests[i].name, "MD5"));
+      TEST_CHECK(algorithm != HSH_MD5);
 #ifdef FEAT_SECHASH
-      TEST_CHECK(strcmp(tests[i].name, "SHA1"));
-      TEST_CHECK(strcmp(tests[i].name, "SHA256"));
-      TEST_CHECK(strcmp(tests[i].name, "SHA384"));
-      TEST_CHECK(strcmp(tests[i].name, "SHA512"));
+      TEST_CHECK(algorithm != HSH_SHA1);
+      TEST_CHECK(algorithm != HSH_SHA256);
+      TEST_CHECK(algorithm != HSH_SHA384);
+      TEST_CHECK(algorithm != HSH_SHA512);
 #endif
       continue;
     }
@@ -88,8 +94,8 @@ test_unit(void)
     DEBUG_LOG("testing %s", tests[i].name);
 
     for (j = 0; j <= sizeof (out); j++) {
-      TEST_CHECK(HSH_GetHashId(tests[i].name) == hash_id);
-      TEST_CHECK(HSH_GetHashId("nosuchhash") < 0);
+      TEST_CHECK(HSH_GetHashId(algorithm) == hash_id);
+      TEST_CHECK(HSH_GetHashId(0) < 0);
 
       memset(out, 0, sizeof (out));
       length = HSH_Hash(hash_id, data1, sizeof (data1) - 1, data2, sizeof (data2) - 1,
diff --git a/util.c b/util.c
index 8f18a216b7435c251232b614122ab334a1341e30..a71b8fc2d4a187aef35045aa8e88c194cbf8f323 100644 (file)
--- a/util.c
+++ b/util.c
@@ -399,7 +399,7 @@ UTI_IPToRefid(const IPAddr *ip)
       return ip->addr.in4;
     case IPADDR_INET6:
       if (MD5_hash < 0)
-        MD5_hash = HSH_GetHashId("MD5");
+        MD5_hash = HSH_GetHashId(HSH_MD5);
 
       if (MD5_hash < 0 ||
           HSH_Hash(MD5_hash, (const unsigned char *)ip->addr.in6, sizeof (ip->addr.in6),
@@ -927,6 +927,36 @@ UTI_FloatHostToNetwork(double x)
 
 /* ================================================== */
 
+HSH_Algorithm
+UTI_HashNameToAlgorithm(const char *name)
+{
+  if (strcmp(name, "MD5") == 0)
+    return HSH_MD5;
+  else if (strcmp(name, "SHA1") == 0)
+    return HSH_SHA1;
+  else if (strcmp(name, "SHA256") == 0)
+    return HSH_SHA256;
+  else if (strcmp(name, "SHA384") == 0)
+    return HSH_SHA384;
+  else if (strcmp(name, "SHA512") == 0)
+    return HSH_SHA512;
+  else if (strcmp(name, "SHA3-224") == 0)
+    return HSH_SHA3_224;
+  else if (strcmp(name, "SHA3-256") == 0)
+    return HSH_SHA3_256;
+  else if (strcmp(name, "SHA3-384") == 0)
+    return HSH_SHA3_384;
+  else if (strcmp(name, "SHA3-512") == 0)
+    return HSH_SHA3_512;
+  else if (strcmp(name, "TIGER") == 0)
+    return HSH_TIGER;
+  else if (strcmp(name, "WHIRLPOOL") == 0)
+    return HSH_WHIRLPOOL;
+  return HSH_INVALID;
+}
+
+/* ================================================== */
+
 int
 UTI_FdSetCloexec(int fd)
 {
diff --git a/util.h b/util.h
index 1e28b26fc15e55ddeae4df28194fbcd8ac635c0d..f3cb99f1d46f4da0f5bd13bfb9f7cf9b2e950fc4 100644 (file)
--- a/util.h
+++ b/util.h
@@ -165,6 +165,8 @@ extern void UTI_TimespecHostToNetwork(const struct timespec *src, Timespec *dest
 extern double UTI_FloatNetworkToHost(Float x);
 extern Float UTI_FloatHostToNetwork(double x);
 
+extern HSH_Algorithm UTI_HashNameToAlgorithm(const char *name);
+
 /* Set FD_CLOEXEC on descriptor */
 extern int UTI_FdSetCloexec(int fd);