summaryrefslogtreecommitdiffstats
path: root/hash_nss.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-05 18:43:21 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-05 18:43:21 +0000
commit104f986b0650b8f93540785d2bcf486905e49b62 (patch)
tree2b2ae5113d9b57425d4bb3f726e325316b87e00a /hash_nss.c
parentInitial commit. (diff)
downloadchrony-upstream/3.4.tar.xz
chrony-upstream/3.4.zip
Adding upstream version 3.4.upstream/3.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'hash_nss.c')
-rw-r--r--hash_nss.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/hash_nss.c b/hash_nss.c
new file mode 100644
index 0000000..9967bf8
--- /dev/null
+++ b/hash_nss.c
@@ -0,0 +1,109 @@
+/*
+ chronyd/chronyc - Programs for keeping computer clocks accurate.
+
+ **********************************************************************
+ * Copyright (C) Miroslav Lichvar 2012
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ **********************************************************************
+
+ =======================================================================
+
+ Routines implementing crypto hashing using NSSLOWHASH API of the NSS library.
+
+ */
+
+#include "config.h"
+
+#include <nss.h>
+#include <hasht.h>
+#include <nsslowhash.h>
+
+#include "hash.h"
+#include "util.h"
+
+static NSSLOWInitContext *ictx;
+
+struct hash {
+ HASH_HashType type;
+ const char *name;
+ 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 }
+};
+
+int
+HSH_GetHashId(const char *name)
+{
+ int i;
+
+ for (i = 0; hashes[i].name; i++) {
+ if (!strcmp(name, hashes[i].name))
+ break;
+ }
+
+ if (!hashes[i].name)
+ return -1; /* not found */
+
+ if (!ictx && !(ictx = NSSLOW_Init()))
+ return -1; /* couldn't init NSS */
+
+ if (!hashes[i].context &&
+ !(hashes[i].context = NSSLOWHASH_NewContext(ictx, hashes[i].type)))
+ return -1; /* couldn't init hash */
+
+ return i;
+}
+
+unsigned int
+HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
+ const unsigned char *in2, unsigned int in2_len,
+ unsigned char *out, unsigned int out_len)
+{
+ unsigned char buf[MAX_HASH_LENGTH];
+ unsigned int ret = 0;
+
+ NSSLOWHASH_Begin(hashes[id].context);
+ NSSLOWHASH_Update(hashes[id].context, in1, in1_len);
+ if (in2)
+ NSSLOWHASH_Update(hashes[id].context, in2, in2_len);
+ NSSLOWHASH_End(hashes[id].context, buf, &ret, sizeof (buf));
+
+ ret = MIN(ret, out_len);
+ memcpy(out, buf, ret);
+
+ return ret;
+}
+
+void
+HSH_Finalise(void)
+{
+ int i;
+
+ for (i = 0; hashes[i].name; i++) {
+ if (hashes[i].context)
+ NSSLOWHASH_Destroy(hashes[i].context);
+ }
+
+ if (ictx)
+ NSSLOW_Shutdown(ictx);
+}