Blame SOURCES/Add-a-hash-table-implementation-to-libkrb5support.patch

6d55b5
From b3f5d3b0542ae314acfb94e1bc5a8f22201b8ac3 Mon Sep 17 00:00:00 2001
6d55b5
From: Greg Hudson <ghudson@mit.edu>
6d55b5
Date: Sat, 4 Aug 2018 20:11:09 -0400
6d55b5
Subject: [PATCH] Add a hash table implementation to libkrb5support
6d55b5
6d55b5
(cherry picked from commit 09e814fe47f5ceeb35bee15ced6e346db8a5e81d)
6d55b5
[rharwood@redhat.com: gitignore, no utf16]
6d55b5
---
6d55b5
 src/include/k5-hashtab.h                      |  79 ++++++
6d55b5
 src/util/support/Makefile.in                  |  15 +-
6d55b5
 src/util/support/deps                         |  11 +
6d55b5
 src/util/support/hashtab.c                    | 243 ++++++++++++++++++
6d55b5
 src/util/support/libkrb5support-fixed.exports |   5 +
6d55b5
 src/util/support/t_hashtab.c                  | 176 +++++++++++++
6d55b5
 6 files changed, 526 insertions(+), 3 deletions(-)
6d55b5
 create mode 100644 src/include/k5-hashtab.h
6d55b5
 create mode 100644 src/util/support/hashtab.c
6d55b5
 create mode 100644 src/util/support/t_hashtab.c
6d55b5
6d55b5
diff --git a/src/include/k5-hashtab.h b/src/include/k5-hashtab.h
6d55b5
new file mode 100644
6d55b5
index 000000000..dc0ef3613
6d55b5
--- /dev/null
6d55b5
+++ b/src/include/k5-hashtab.h
6d55b5
@@ -0,0 +1,79 @@
6d55b5
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
6d55b5
+/* include/k5-hash.h - hash table interface definitions */
6d55b5
+/*
6d55b5
+ * Copyright (C) 2018 by the Massachusetts Institute of Technology.
6d55b5
+ * All rights reserved.
6d55b5
+ *
6d55b5
+ * Redistribution and use in source and binary forms, with or without
6d55b5
+ * modification, are permitted provided that the following conditions
6d55b5
+ * are met:
6d55b5
+ *
6d55b5
+ * * Redistributions of source code must retain the above copyright
6d55b5
+ *   notice, this list of conditions and the following disclaimer.
6d55b5
+ *
6d55b5
+ * * Redistributions in binary form must reproduce the above copyright
6d55b5
+ *   notice, this list of conditions and the following disclaimer in
6d55b5
+ *   the documentation and/or other materials provided with the
6d55b5
+ *   distribution.
6d55b5
+ *
6d55b5
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6d55b5
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6d55b5
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
6d55b5
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
6d55b5
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
6d55b5
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
6d55b5
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
6d55b5
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6d55b5
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
6d55b5
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6d55b5
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
6d55b5
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
6d55b5
+ */
6d55b5
+
6d55b5
+/*
6d55b5
+ * This file contains declarations for a simple hash table using siphash.  Some
6d55b5
+ * limitations which might need to be addressed in the future:
6d55b5
+ *
6d55b5
+ * - The table does not manage caller memory.  This limitation could be
6d55b5
+ *   addressed by adding an optional free callback to k5_hashtab_create(), to
6d55b5
+ *   be called by k5_hashtab_free() and k5_hashtab_remove().
6d55b5
+ *
6d55b5
+ * - There is no way to iterate over a hash table.
6d55b5
+ *
6d55b5
+ * - k5_hashtab_add() does not check for duplicate entries.
6d55b5
+ */
6d55b5
+
6d55b5
+#ifndef K5_HASH_H
6d55b5
+#define K5_HASH_H
6d55b5
+
6d55b5
+#define K5_HASH_SEED_LEN 16
6d55b5
+
6d55b5
+struct k5_hashtab;
6d55b5
+
6d55b5
+/*
6d55b5
+ * Create a new hash table in *ht_out.  seed must point to random bytes if keys
6d55b5
+ * might be under the control of an attacker; otherwise it may be NULL.
6d55b5
+ * initial_buckets controls the initial allocation of hash buckets; pass zero
6d55b5
+ * to use a default value.  The number of hash buckets will be doubled as the
6d55b5
+ * number of entries increases.  Return 0 on success, ENOMEM on failure.
6d55b5
+ */
6d55b5
+int k5_hashtab_create(const uint8_t seed[K5_HASH_SEED_LEN],
6d55b5
+                      size_t initial_buckets, struct k5_hashtab **ht_out);
6d55b5
+
6d55b5
+/* Release the memory used by a hash table.  Keys and values are the caller's
6d55b5
+ * responsibility. */
6d55b5
+void k5_hashtab_free(struct k5_hashtab *ht);
6d55b5
+
6d55b5
+/* Add an entry to a hash table.  key and val must remain valid until the entry
6d55b5
+ * is removed or the hash table is freed.  The caller must avoid duplicates. */
6d55b5
+int k5_hashtab_add(struct k5_hashtab *ht, const void *key, size_t klen,
6d55b5
+                   void *val);
6d55b5
+
6d55b5
+/* Remove an entry from a hash table by key.  Does not free key or the
6d55b5
+ * associated value.  Return 1 if the key was found and removed, 0 if not. */
6d55b5
+int k5_hashtab_remove(struct k5_hashtab *ht, const void *key, size_t klen);
6d55b5
+
6d55b5
+/* Retrieve a value from a hash table by key. */
6d55b5
+void *k5_hashtab_get(struct k5_hashtab *ht, const void *key, size_t klen);
6d55b5
+
6d55b5
+#endif /* K5_HASH_H */
6d55b5
diff --git a/src/util/support/Makefile.in b/src/util/support/Makefile.in
6d55b5
index b3576f0b7..12797068f 100644
6d55b5
--- a/src/util/support/Makefile.in
6d55b5
+++ b/src/util/support/Makefile.in
6d55b5
@@ -83,6 +83,7 @@ STLIBOBJS= \
6d55b5
 	base64.o \
6d55b5
 	json.o \
6d55b5
 	hex.o \
6d55b5
+	hashtab.o \
6d55b5
 	bcmp.o \
6d55b5
 	strerror_r.o \
6d55b5
 	dir_filenames.o \
6d55b5
@@ -110,6 +111,7 @@ LIBOBJS= \
6d55b5
 	$(OUTPRE)base64.$(OBJEXT) \
6d55b5
 	$(OUTPRE)json.$(OBJEXT) \
6d55b5
 	$(OUTPRE)hex.$(OBJEXT) \
6d55b5
+	$(OUTPRE)hashtab.$(OBJEXT) \
6d55b5
 	$(OUTPRE)bcmp.$(OBJEXT) \
6d55b5
 	$(OUTPRE)strerror_r.$(OBJEXT) \
6d55b5
 	$(OUTPRE)dir_filenames.$(OBJEXT) \
6d55b5
@@ -142,11 +144,13 @@ SRCS=\
6d55b5
 	$(srcdir)/t_path.c \
6d55b5
 	$(srcdir)/t_json.c \
6d55b5
 	$(srcdir)/t_hex.c \
6d55b5
+	$(srcdir)/t_hashtab.c \
6d55b5
 	$(srcdir)/zap.c \
6d55b5
 	$(srcdir)/path.c \
6d55b5
 	$(srcdir)/base64.c \
6d55b5
 	$(srcdir)/json.c \
6d55b5
 	$(srcdir)/hex.c \
6d55b5
+	$(srcdir)/hashtab.c \
6d55b5
 	$(srcdir)/bcmp.c \
6d55b5
 	$(srcdir)/strerror_r.c \
6d55b5
 	$(srcdir)/dir_filenames.c \
6d55b5
@@ -225,13 +229,17 @@ t_json: $(T_JSON_OBJS)
6d55b5
 t_hex: t_hex.o hex.o
6d55b5
 	$(CC_LINK) -o $@ t_hex.o hex.o
6d55b5
 
6d55b5
+t_hashtab: t_hashtab.o
6d55b5
+	$(CC_LINK) -o $@ t_hashtab.o
6d55b5
+
6d55b5
 t_unal: t_unal.o
6d55b5
 	$(CC_LINK) -o t_unal t_unal.o
6d55b5
 
6d55b5
 t_utf8: t_utf8.o utf8.o
6d55b5
 	$(CC_LINK) -o t_utf8 t_utf8.o utf8.o
6d55b5
 
6d55b5
-TEST_PROGS= t_k5buf t_path t_path_win t_base64 t_json t_hex t_unal t_utf8
6d55b5
+TEST_PROGS= t_k5buf t_path t_path_win t_base64 t_json t_hex t_hashtab t_unal \
6d55b5
+	t_utf8
6d55b5
 
6d55b5
 check-unix: $(TEST_PROGS)
6d55b5
 	./t_k5buf
6d55b5
@@ -240,14 +248,15 @@ check-unix: $(TEST_PROGS)
6d55b5
 	./t_base64
6d55b5
 	./t_json
6d55b5
 	./t_hex
6d55b5
+	./t_hashtab
6d55b5
 	./t_unal
6d55b5
 	./t_utf8
6d55b5
 
6d55b5
 clean:
6d55b5
 	$(RM) t_k5buf.o t_k5buf t_unal.o t_unal path_win.o path_win
6d55b5
 	$(RM) t_path_win.o t_path_win t_path.o t_path t_base64.o t_base64
6d55b5
-	$(RM) t_json.o t_json t_hex.o t_hex libkrb5support.exports
6d55b5
-	$(RM) t_utf8.o t_utf8
6d55b5
+	$(RM) t_json.o t_json t_hex.o t_hex t_hashtab.o t_hashtab
6d55b5
+	$(RM) t_utf8.o t_utf8 libkrb5support.exports
6d55b5
 
6d55b5
 @lib_frag@
6d55b5
 @libobj_frag@
6d55b5
diff --git a/src/util/support/deps b/src/util/support/deps
6d55b5
index 551843357..d43baea13 100644
6d55b5
--- a/src/util/support/deps
6d55b5
+++ b/src/util/support/deps
6d55b5
@@ -65,6 +65,10 @@ t_json.so t_json.po $(OUTPRE)t_json.$(OBJEXT): $(top_srcdir)/include/k5-json.h \
6d55b5
 t_hex.so t_hex.po $(OUTPRE)t_hex.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
6d55b5
   $(top_srcdir)/include/k5-hex.h $(top_srcdir)/include/k5-platform.h \
6d55b5
   $(top_srcdir)/include/k5-thread.h t_hex.c
6d55b5
+t_hashtab.so t_hashtab.po $(OUTPRE)t_hashtab.$(OBJEXT): \
6d55b5
+  $(BUILDTOP)/include/autoconf.h $(top_srcdir)/include/k5-hashtab.h \
6d55b5
+  $(top_srcdir)/include/k5-platform.h $(top_srcdir)/include/k5-queue.h \
6d55b5
+  $(top_srcdir)/include/k5-thread.h hashtab.c t_hashtab.c
6d55b5
 zap.so zap.po $(OUTPRE)zap.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
6d55b5
   $(top_srcdir)/include/k5-platform.h $(top_srcdir)/include/k5-thread.h \
6d55b5
   zap.c
6d55b5
@@ -81,12 +85,19 @@ json.so json.po $(OUTPRE)json.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
6d55b5
 hex.so hex.po $(OUTPRE)hex.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
6d55b5
   $(top_srcdir)/include/k5-hex.h $(top_srcdir)/include/k5-platform.h \
6d55b5
   $(top_srcdir)/include/k5-thread.h hex.c
6d55b5
+hashtab.so hashtab.po $(OUTPRE)hashtab.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
6d55b5
+  $(top_srcdir)/include/k5-hashtab.h $(top_srcdir)/include/k5-platform.h \
6d55b5
+  $(top_srcdir)/include/k5-queue.h $(top_srcdir)/include/k5-thread.h \
6d55b5
+  hashtab.c
6d55b5
 bcmp.so bcmp.po $(OUTPRE)bcmp.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
6d55b5
   $(top_srcdir)/include/k5-platform.h $(top_srcdir)/include/k5-thread.h \
6d55b5
   bcmp.c
6d55b5
 strerror_r.so strerror_r.po $(OUTPRE)strerror_r.$(OBJEXT): \
6d55b5
   $(BUILDTOP)/include/autoconf.h $(top_srcdir)/include/k5-platform.h \
6d55b5
   $(top_srcdir)/include/k5-thread.h strerror_r.c
6d55b5
+dir_filenames.so dir_filenames.po $(OUTPRE)dir_filenames.$(OBJEXT): \
6d55b5
+  $(BUILDTOP)/include/autoconf.h $(top_srcdir)/include/k5-platform.h \
6d55b5
+  $(top_srcdir)/include/k5-thread.h dir_filenames.c
6d55b5
 t_utf8.so t_utf8.po $(OUTPRE)t_utf8.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
6d55b5
   $(top_srcdir)/include/k5-platform.h $(top_srcdir)/include/k5-thread.h \
6d55b5
   $(top_srcdir)/include/k5-utf8.h t_utf8.c
6d55b5
diff --git a/src/util/support/hashtab.c b/src/util/support/hashtab.c
6d55b5
new file mode 100644
6d55b5
index 000000000..e04e491b2
6d55b5
--- /dev/null
6d55b5
+++ b/src/util/support/hashtab.c
6d55b5
@@ -0,0 +1,243 @@
6d55b5
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
6d55b5
+/* util/support/hash.c - hash table implementation */
6d55b5
+/*
6d55b5
+ * Copyright (C) 2018 by the Massachusetts Institute of Technology.
6d55b5
+ * All rights reserved.
6d55b5
+ *
6d55b5
+ * Redistribution and use in source and binary forms, with or without
6d55b5
+ * modification, are permitted provided that the following conditions
6d55b5
+ * are met:
6d55b5
+ *
6d55b5
+ * * Redistributions of source code must retain the above copyright
6d55b5
+ *   notice, this list of conditions and the following disclaimer.
6d55b5
+ *
6d55b5
+ * * Redistributions in binary form must reproduce the above copyright
6d55b5
+ *   notice, this list of conditions and the following disclaimer in
6d55b5
+ *   the documentation and/or other materials provided with the
6d55b5
+ *   distribution.
6d55b5
+ *
6d55b5
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6d55b5
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6d55b5
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
6d55b5
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
6d55b5
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
6d55b5
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
6d55b5
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
6d55b5
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6d55b5
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
6d55b5
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6d55b5
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
6d55b5
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
6d55b5
+ */
6d55b5
+
6d55b5
+#include "k5-platform.h"
6d55b5
+#include "k5-hashtab.h"
6d55b5
+#include "k5-queue.h"
6d55b5
+
6d55b5
+struct entry {
6d55b5
+    const void *key;
6d55b5
+    size_t klen;
6d55b5
+    void *val;
6d55b5
+    K5_SLIST_ENTRY(entry) next;
6d55b5
+};
6d55b5
+
6d55b5
+struct k5_hashtab {
6d55b5
+    uint64_t k0;
6d55b5
+    uint64_t k1;
6d55b5
+    size_t nbuckets;
6d55b5
+    size_t nentries;
6d55b5
+    K5_SLIST_HEAD(bucket_list, entry) *buckets;
6d55b5
+};
6d55b5
+
6d55b5
+/* Return x rotated to the left by r bits. */
6d55b5
+static inline uint64_t
6d55b5
+rotl64(uint64_t x, int r)
6d55b5
+{
6d55b5
+    return (x << r) | (x >> (64 - r));
6d55b5
+}
6d55b5
+
6d55b5
+static inline void
6d55b5
+sipround(uint64_t *v0, uint64_t *v1, uint64_t *v2, uint64_t *v3)
6d55b5
+{
6d55b5
+    *v0 += *v1;
6d55b5
+    *v2 += *v3;
6d55b5
+    *v1 = rotl64(*v1, 13) ^ *v0;
6d55b5
+    *v3 = rotl64(*v3, 16) ^ *v2;
6d55b5
+    *v0 = rotl64(*v0, 32);
6d55b5
+    *v2 += *v1;
6d55b5
+    *v0 += *v3;
6d55b5
+    *v1 = rotl64(*v1, 17) ^ *v2;
6d55b5
+    *v3 = rotl64(*v3, 21) ^ *v0;
6d55b5
+    *v2 = rotl64(*v2, 32);
6d55b5
+}
6d55b5
+
6d55b5
+/* SipHash-2-4 from https://131002.net/siphash/siphash.pdf (Jean-Philippe
6d55b5
+ * Aumasson and Daniel J. Bernstein) */
6d55b5
+static uint64_t
6d55b5
+siphash24(const uint8_t *data, size_t len, uint64_t k0, uint64_t k1)
6d55b5
+{
6d55b5
+    uint64_t v0 = k0 ^ 0x736F6D6570736575;
6d55b5
+    uint64_t v1 = k1 ^ 0x646F72616E646F6D;
6d55b5
+    uint64_t v2 = k0 ^ 0x6C7967656E657261;
6d55b5
+    uint64_t v3 = k1 ^ 0x7465646279746573;
6d55b5
+    uint64_t mi;
6d55b5
+    const uint8_t *p, *end = data + (len - len % 8);
6d55b5
+    uint8_t last[8] = { 0 };
6d55b5
+
6d55b5
+    /* Process each full 8-byte chunk of data. */
6d55b5
+    for (p = data; p < end; p += 8) {
6d55b5
+        mi = load_64_le(p);
6d55b5
+        v3 ^= mi;
6d55b5
+        sipround(&v0, &v1, &v2, &v3;;
6d55b5
+        sipround(&v0, &v1, &v2, &v3;;
6d55b5
+        v0 ^= mi;
6d55b5
+    }
6d55b5
+
6d55b5
+    /* Process the last 0-7 bytes followed by the length mod 256. */
6d55b5
+    memcpy(last, end, len % 8);
6d55b5
+    last[7] = len & 0xFF;
6d55b5
+    mi = load_64_le(last);
6d55b5
+    v3 ^= mi;
6d55b5
+    sipround(&v0, &v1, &v2, &v3;;
6d55b5
+    sipround(&v0, &v1, &v2, &v3;;
6d55b5
+    v0 ^= mi;
6d55b5
+
6d55b5
+    /* Finalize. */
6d55b5
+    v2 ^= 0xFF;
6d55b5
+    sipround(&v0, &v1, &v2, &v3;;
6d55b5
+    sipround(&v0, &v1, &v2, &v3;;
6d55b5
+    sipround(&v0, &v1, &v2, &v3;;
6d55b5
+    sipround(&v0, &v1, &v2, &v3;;
6d55b5
+    return v0 ^ v1 ^ v2 ^ v3;
6d55b5
+}
6d55b5
+
6d55b5
+int
6d55b5
+k5_hashtab_create(const uint8_t seed[K5_HASH_SEED_LEN], size_t initial_buckets,
6d55b5
+                  struct k5_hashtab **ht_out)
6d55b5
+{
6d55b5
+    struct k5_hashtab *ht;
6d55b5
+
6d55b5
+    *ht_out = NULL;
6d55b5
+
6d55b5
+    ht = malloc(sizeof(*ht));
6d55b5
+    if (ht == NULL)
6d55b5
+        return ENOMEM;
6d55b5
+
6d55b5
+    if (seed != NULL) {
6d55b5
+        ht->k0 = load_64_le(seed);
6d55b5
+        ht->k1 = load_64_le(seed + 8);
6d55b5
+    } else {
6d55b5
+        ht->k0 = ht->k1 = 0;
6d55b5
+    }
6d55b5
+    ht->nbuckets = (initial_buckets > 0) ? initial_buckets : 64;
6d55b5
+    ht->nentries = 0;
6d55b5
+    ht->buckets = calloc(ht->nbuckets, sizeof(*ht->buckets));
6d55b5
+    if (ht->buckets == NULL) {
6d55b5
+        free(ht);
6d55b5
+        return ENOMEM;
6d55b5
+    }
6d55b5
+
6d55b5
+    *ht_out = ht;
6d55b5
+    return 0;
6d55b5
+}
6d55b5
+
6d55b5
+void
6d55b5
+k5_hashtab_free(struct k5_hashtab *ht)
6d55b5
+{
6d55b5
+    size_t i;
6d55b5
+    struct entry *ent;
6d55b5
+
6d55b5
+    for (i = 0; i < ht->nbuckets; i++) {
6d55b5
+        while (!K5_SLIST_EMPTY(&ht->buckets[i])) {
6d55b5
+            ent = K5_SLIST_FIRST(&ht->buckets[i]);
6d55b5
+            K5_SLIST_REMOVE_HEAD(&ht->buckets[i], next);
6d55b5
+            free(ent);
6d55b5
+        }
6d55b5
+    }
6d55b5
+    free(ht->buckets);
6d55b5
+    free(ht);
6d55b5
+}
6d55b5
+
6d55b5
+static int
6d55b5
+resize_table(struct k5_hashtab *ht)
6d55b5
+{
6d55b5
+    size_t i, j, newsize = ht->nbuckets * 2;
6d55b5
+    struct bucket_list *newbuckets;
6d55b5
+    struct entry *ent;
6d55b5
+
6d55b5
+    newbuckets = calloc(newsize, sizeof(*newbuckets));
6d55b5
+    if (newbuckets == NULL)
6d55b5
+        return ENOMEM;
6d55b5
+
6d55b5
+    /* Rehash all the entries into the new buckets. */
6d55b5
+    for (i = 0; i < ht->nbuckets; i++) {
6d55b5
+        while (!K5_SLIST_EMPTY(&ht->buckets[i])) {
6d55b5
+            ent = K5_SLIST_FIRST(&ht->buckets[i]);
6d55b5
+            j = siphash24(ent->key, ent->klen, ht->k0, ht->k1) % newsize;
6d55b5
+            K5_SLIST_REMOVE_HEAD(&ht->buckets[i], next);
6d55b5
+            K5_SLIST_INSERT_HEAD(&newbuckets[j], ent, next);
6d55b5
+        }
6d55b5
+    }
6d55b5
+
6d55b5
+    free(ht->buckets);
6d55b5
+    ht->buckets = newbuckets;
6d55b5
+    ht->nbuckets = newsize;
6d55b5
+    return 0;
6d55b5
+}
6d55b5
+
6d55b5
+int
6d55b5
+k5_hashtab_add(struct k5_hashtab *ht, const void *key, size_t klen, void *val)
6d55b5
+{
6d55b5
+    size_t i;
6d55b5
+    struct entry *ent;
6d55b5
+
6d55b5
+    if (ht->nentries == ht->nbuckets) {
6d55b5
+        if (resize_table(ht) != 0)
6d55b5
+            return ENOMEM;
6d55b5
+    }
6d55b5
+
6d55b5
+    ent = malloc(sizeof(*ent));
6d55b5
+    if (ent == NULL)
6d55b5
+        return ENOMEM;
6d55b5
+    ent->key = key;
6d55b5
+    ent->klen = klen;
6d55b5
+    ent->val = val;
6d55b5
+
6d55b5
+    i = siphash24(key, klen, ht->k0, ht->k1) % ht->nbuckets;
6d55b5
+    K5_SLIST_INSERT_HEAD(&ht->buckets[i], ent, next);
6d55b5
+
6d55b5
+    ht->nentries++;
6d55b5
+    return 0;
6d55b5
+}
6d55b5
+
6d55b5
+int
6d55b5
+k5_hashtab_remove(struct k5_hashtab *ht, const void *key, size_t klen)
6d55b5
+{
6d55b5
+    size_t i;
6d55b5
+    struct entry *ent;
6d55b5
+
6d55b5
+    i = siphash24(key, klen, ht->k0, ht->k1) % ht->nbuckets;
6d55b5
+    K5_SLIST_FOREACH(ent, &ht->buckets[i], next) {
6d55b5
+        if (ent->klen == klen && memcmp(ent->key, key, klen) == 0) {
6d55b5
+            K5_SLIST_REMOVE(&ht->buckets[i], ent, entry, next);
6d55b5
+            free(ent);
6d55b5
+            ht->nentries--;
6d55b5
+            return 1;
6d55b5
+        }
6d55b5
+    }
6d55b5
+    return 0;
6d55b5
+}
6d55b5
+
6d55b5
+void *
6d55b5
+k5_hashtab_get(struct k5_hashtab *ht, const void *key, size_t klen)
6d55b5
+{
6d55b5
+    size_t i;
6d55b5
+    struct entry *ent;
6d55b5
+
6d55b5
+    i = siphash24(key, klen, ht->k0, ht->k1) % ht->nbuckets;
6d55b5
+    K5_SLIST_FOREACH(ent, &ht->buckets[i], next) {
6d55b5
+        if (ent->klen == klen && memcmp(ent->key, key, klen) == 0)
6d55b5
+            return ent->val;
6d55b5
+    }
6d55b5
+    return NULL;
6d55b5
+}
6d55b5
diff --git a/src/util/support/libkrb5support-fixed.exports b/src/util/support/libkrb5support-fixed.exports
6d55b5
index 6193d7331..c63c5fbc3 100644
6d55b5
--- a/src/util/support/libkrb5support-fixed.exports
6d55b5
+++ b/src/util/support/libkrb5support-fixed.exports
6d55b5
@@ -16,6 +16,11 @@ k5_get_error
6d55b5
 k5_free_error
6d55b5
 k5_clear_error
6d55b5
 k5_set_error_info_callout_fn
6d55b5
+k5_hashtab_add
6d55b5
+k5_hashtab_create
6d55b5
+k5_hashtab_free
6d55b5
+k5_hashtab_get
6d55b5
+k5_hashtab_remove
6d55b5
 k5_hex_decode
6d55b5
 k5_hex_encode
6d55b5
 k5_json_array_add
6d55b5
diff --git a/src/util/support/t_hashtab.c b/src/util/support/t_hashtab.c
6d55b5
new file mode 100644
6d55b5
index 000000000..f51abc4f1
6d55b5
--- /dev/null
6d55b5
+++ b/src/util/support/t_hashtab.c
6d55b5
@@ -0,0 +1,176 @@
6d55b5
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
6d55b5
+/* util/support/t_hash.c - tests for hash table code */
6d55b5
+/*
6d55b5
+ * Copyright (C) 2018 by the Massachusetts Institute of Technology.
6d55b5
+ * All rights reserved.
6d55b5
+ *
6d55b5
+ * Redistribution and use in source and binary forms, with or without
6d55b5
+ * modification, are permitted provided that the following conditions
6d55b5
+ * are met:
6d55b5
+ *
6d55b5
+ * * Redistributions of source code must retain the above copyright
6d55b5
+ *   notice, this list of conditions and the following disclaimer.
6d55b5
+ *
6d55b5
+ * * Redistributions in binary form must reproduce the above copyright
6d55b5
+ *   notice, this list of conditions and the following disclaimer in
6d55b5
+ *   the documentation and/or other materials provided with the
6d55b5
+ *   distribution.
6d55b5
+ *
6d55b5
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6d55b5
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6d55b5
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
6d55b5
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
6d55b5
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
6d55b5
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
6d55b5
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
6d55b5
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6d55b5
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
6d55b5
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6d55b5
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
6d55b5
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
6d55b5
+ */
6d55b5
+
6d55b5
+/* hash.c has no linker dependencies, so we can simply include its source code
6d55b5
+ * to test its static functions and look inside its structures. */
6d55b5
+#include "hashtab.c"
6d55b5
+
6d55b5
+/* These match the sip64 test vectors in the reference C implementation of
6d55b5
+ * siphash at https://github.com/veorq/SipHash */
6d55b5
+const uint64_t vectors[64] = {
6d55b5
+    0x726FDB47DD0E0E31,
6d55b5
+    0x74F839C593DC67FD,
6d55b5
+    0x0D6C8009D9A94F5A,
6d55b5
+    0x85676696D7FB7E2D,
6d55b5
+    0xCF2794E0277187B7,
6d55b5
+    0x18765564CD99A68D,
6d55b5
+    0xCBC9466E58FEE3CE,
6d55b5
+    0xAB0200F58B01D137,
6d55b5
+    0x93F5F5799A932462,
6d55b5
+    0x9E0082DF0BA9E4B0,
6d55b5
+    0x7A5DBBC594DDB9F3,
6d55b5
+    0xF4B32F46226BADA7,
6d55b5
+    0x751E8FBC860EE5FB,
6d55b5
+    0x14EA5627C0843D90,
6d55b5
+    0xF723CA908E7AF2EE,
6d55b5
+    0xA129CA6149BE45E5,
6d55b5
+    0x3F2ACC7F57C29BDB,
6d55b5
+    0x699AE9F52CBE4794,
6d55b5
+    0x4BC1B3F0968DD39C,
6d55b5
+    0xBB6DC91DA77961BD,
6d55b5
+    0xBED65CF21AA2EE98,
6d55b5
+    0xD0F2CBB02E3B67C7,
6d55b5
+    0x93536795E3A33E88,
6d55b5
+    0xA80C038CCD5CCEC8,
6d55b5
+    0xB8AD50C6F649AF94,
6d55b5
+    0xBCE192DE8A85B8EA,
6d55b5
+    0x17D835B85BBB15F3,
6d55b5
+    0x2F2E6163076BCFAD,
6d55b5
+    0xDE4DAAACA71DC9A5,
6d55b5
+    0xA6A2506687956571,
6d55b5
+    0xAD87A3535C49EF28,
6d55b5
+    0x32D892FAD841C342,
6d55b5
+    0x7127512F72F27CCE,
6d55b5
+    0xA7F32346F95978E3,
6d55b5
+    0x12E0B01ABB051238,
6d55b5
+    0x15E034D40FA197AE,
6d55b5
+    0x314DFFBE0815A3B4,
6d55b5
+    0x027990F029623981,
6d55b5
+    0xCADCD4E59EF40C4D,
6d55b5
+    0x9ABFD8766A33735C,
6d55b5
+    0x0E3EA96B5304A7D0,
6d55b5
+    0xAD0C42D6FC585992,
6d55b5
+    0x187306C89BC215A9,
6d55b5
+    0xD4A60ABCF3792B95,
6d55b5
+    0xF935451DE4F21DF2,
6d55b5
+    0xA9538F0419755787,
6d55b5
+    0xDB9ACDDFF56CA510,
6d55b5
+    0xD06C98CD5C0975EB,
6d55b5
+    0xE612A3CB9ECBA951,
6d55b5
+    0xC766E62CFCADAF96,
6d55b5
+    0xEE64435A9752FE72,
6d55b5
+    0xA192D576B245165A,
6d55b5
+    0x0A8787BF8ECB74B2,
6d55b5
+    0x81B3E73D20B49B6F,
6d55b5
+    0x7FA8220BA3B2ECEA,
6d55b5
+    0x245731C13CA42499,
6d55b5
+    0xB78DBFAF3A8D83BD,
6d55b5
+    0xEA1AD565322A1A0B,
6d55b5
+    0x60E61C23A3795013,
6d55b5
+    0x6606D7E446282B93,
6d55b5
+    0x6CA4ECB15C5F91E1,
6d55b5
+    0x9F626DA15C9625F3,
6d55b5
+    0xE51B38608EF25F57,
6d55b5
+    0x958A324CEB064572
6d55b5
+};
6d55b5
+
6d55b5
+static void
6d55b5
+test_siphash()
6d55b5
+{
6d55b5
+    uint8_t seq[64];
6d55b5
+    uint64_t k0, k1, hval;
6d55b5
+    size_t i;
6d55b5
+
6d55b5
+    for (i = 0; i < sizeof(seq); i++)
6d55b5
+        seq[i] = i;
6d55b5
+    k0 = load_64_le(seq);
6d55b5
+    k1 = load_64_le(seq + 8);
6d55b5
+
6d55b5
+    for (i = 0; i < sizeof(seq); i++) {
6d55b5
+        hval = siphash24(seq, i, k0, k1);
6d55b5
+        assert(hval == vectors[i]);
6d55b5
+    }
6d55b5
+}
6d55b5
+
6d55b5
+static void
6d55b5
+test_hashtab()
6d55b5
+{
6d55b5
+    int st;
6d55b5
+    struct k5_hashtab *ht;
6d55b5
+    size_t i;
6d55b5
+    char zeros[100] = { 0 };
6d55b5
+
6d55b5
+    st = k5_hashtab_create(NULL, 4, &ht;;
6d55b5
+    assert(st == 0 && ht != NULL && ht->nentries == 0);
6d55b5
+
6d55b5
+    st = k5_hashtab_add(ht, "abc", 3, &st);
6d55b5
+    assert(st == 0 && ht->nentries == 1);
6d55b5
+    assert(k5_hashtab_get(ht, "abc", 3) == &st);
6d55b5
+    assert(k5_hashtab_get(ht, "bcde", 4) == NULL);
6d55b5
+
6d55b5
+    st = k5_hashtab_add(ht, "bcde", 4, &ht;;
6d55b5
+    assert(st == 0 && ht->nentries == 2);
6d55b5
+    assert(k5_hashtab_get(ht, "abc", 3) == &st);
6d55b5
+    assert(k5_hashtab_get(ht, "bcde", 4) == &ht;;
6d55b5
+
6d55b5
+    k5_hashtab_remove(ht, "abc", 3);
6d55b5
+    assert(ht->nentries == 1);
6d55b5
+    assert(k5_hashtab_get(ht, "abc", 3) == NULL);
6d55b5
+    assert(k5_hashtab_get(ht, "bcde", 4) == &ht;;
6d55b5
+
6d55b5
+    k5_hashtab_remove(ht, "bcde", 4);
6d55b5
+    assert(ht->nentries == 0);
6d55b5
+    assert(k5_hashtab_get(ht, "abc", 3) == NULL);
6d55b5
+    assert(k5_hashtab_get(ht, "bcde", 4) == NULL);
6d55b5
+
6d55b5
+    for (i = 0; i < sizeof(zeros); i++) {
6d55b5
+        st = k5_hashtab_add(ht, zeros, i, zeros + i);
6d55b5
+        assert(st == 0 && ht->nentries == i + 1 && ht->nbuckets >= i + 1);
6d55b5
+    }
6d55b5
+    for (i = 0; i < sizeof(zeros); i++) {
6d55b5
+        assert(k5_hashtab_get(ht, zeros, i) == zeros + i);
6d55b5
+        k5_hashtab_remove(ht, zeros, i);
6d55b5
+        assert(ht->nentries == sizeof(zeros) - i - 1);
6d55b5
+        if (i > 0)
6d55b5
+            assert(k5_hashtab_get(ht, zeros, i - 1) == NULL);
6d55b5
+    }
6d55b5
+
6d55b5
+    k5_hashtab_free(ht);
6d55b5
+}
6d55b5
+
6d55b5
+int
6d55b5
+main()
6d55b5
+{
6d55b5
+    test_siphash();
6d55b5
+    test_hashtab();
6d55b5
+    return 0;
6d55b5
+}