]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
Introduce hash2 (sha256).
authorAndre Noll <maan@tuebingen.mpg.de>
Thu, 12 Mar 2020 15:06:12 +0000 (16:06 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Thu, 21 Oct 2021 17:52:40 +0000 (19:52 +0200)
This adds a second hash function which will replace sha1. Both openssl
and libgcrypt support sha256, so it is easy to do. There are no users
of the new functions so far, so this patch has no effect yet.

crypt.h
crypt_common.c
gcrypt.c
openssl.c

diff --git a/crypt.h b/crypt.h
index 9623a0035a3b1a89818bd99c3c09c1dac041519b..5ca6a54112b60f6d61b16485248fb28ff708df6b 100644 (file)
--- a/crypt.h
+++ b/crypt.h
@@ -200,3 +200,42 @@ void hash_to_asc(const unsigned char *hash, char *asc);
  * less than or equal to h2, respectively.
  */
 int hash_compare(const unsigned char *h1, const unsigned char *h2);
+
+/** Size of the hash value in bytes. */
+#define HASH2_SIZE 32
+
+/**
+ * Compute the hash2 of the given input data.
+ *
+ * \param data Pointer to the data to compute the hash value from.
+ * \param len The length of \a data in bytes.
+ * \param hash Result pointer.
+ *
+ * \a hash must point to an area at least \p HASH2_SIZE bytes large.
+ *
+ * \sa sha(3), openssl(1).
+ * */
+void hash2_function(const char *data, unsigned long len, unsigned char *hash);
+
+/**
+ * Convert a hash2 value to ascii format.
+ *
+ * \param hash the hash value.
+ * \param asc Result pointer.
+ *
+ * \a asc must point to an area of at least 2 * \p HASH2_SIZE + 1 bytes which
+ * will be filled by the function with the ascii representation of the hash
+ * value given by \a hash, and a terminating \p NULL byte.
+ */
+void hash2_to_asc(const unsigned char *hash, char *asc);
+
+/**
+ * Compare two version 2 hashes.
+ *
+ * \param h1 Pointer to the first hash value.
+ * \param h2 Pointer to the second hash value.
+ *
+ * \return 1, -1, or zero, depending on whether \a h1 is greater than,
+ * less than or equal to h2, respectively.
+ */
+int hash2_compare(const unsigned char *h1, const unsigned char *h2);
index ff24e356ab8d837f8d59d67a3c983d5264376db1..3a44dbdddcd1cf59108ba110c8d83c4fac11d609 100644 (file)
@@ -160,6 +160,31 @@ int hash_compare(const unsigned char *h1, const unsigned char *h2)
        return 0;
 }
 
+void hash2_to_asc(const unsigned char *hash, char *asc)
+{
+       int i;
+       const char hexchar[] = "0123456789abcdef";
+
+       for (i = 0; i < HASH2_SIZE; i++) {
+               asc[2 * i] = hexchar[hash[i] >> 4];
+               asc[2 * i + 1] = hexchar[hash[i] & 0xf];
+       }
+       asc[2 * HASH2_SIZE] = '\0';
+}
+
+int hash2_compare(const unsigned char *h1, const unsigned char *h2)
+{
+       int i;
+
+       for (i = 0; i < HASH2_SIZE; i++) {
+               if (h1[i] < h2[i])
+                       return -1;
+               if (h1[i] > h2[i])
+                       return 1;
+       }
+       return 0;
+}
+
 /**
  * Check header of an openssh private key and compute bignum offset.
  *
index dbe4900862fef83a552d9c60d9ac21d4c8253d5e..506f0bb84e6c199d065942553935dc27a8833d93 100644 (file)
--- a/gcrypt.c
+++ b/gcrypt.c
@@ -46,6 +46,22 @@ void hash_function(const char *data, unsigned long len, unsigned char *hash)
        gcry_md_close(handle);
 }
 
+void hash2_function(const char *data, unsigned long len, unsigned char *hash)
+{
+       gcry_error_t gret;
+       gcry_md_hd_t handle;
+       unsigned char *md;
+
+       gret = gcry_md_open(&handle, GCRY_MD_SHA256, 0);
+       assert(gret == 0);
+       gcry_md_write(handle, data, (size_t)len);
+       gcry_md_final(handle);
+       md = gcry_md_read(handle, GCRY_MD_SHA256);
+       assert(md);
+       memcpy(hash, md, HASH2_SIZE);
+       gcry_md_close(handle);
+}
+
 void get_random_bytes_or_die(unsigned char *buf, int num)
 {
        gcry_randomize(buf, (size_t)num, GCRY_STRONG_RANDOM);
index 0ad9d7db4e7f035dce140365b2b38abc144b6923..32891cbb012b66c0eaf5b8ca56f2c4288cd3bb1b 100644 (file)
--- a/openssl.c
+++ b/openssl.c
@@ -414,3 +414,11 @@ void hash_function(const char *data, unsigned long len, unsigned char *hash)
        SHA1_Update(&c, data, len);
        SHA1_Final(hash, &c);
 }
+
+void hash2_function(const char *data, unsigned long len, unsigned char *hash)
+{
+       SHA256_CTX c;
+       SHA256_Init(&c);
+       SHA256_Update(&c, data, len);
+       SHA256_Final(hash, &c);
+}