summaryrefslogtreecommitdiff
path: root/src/lib/crypt_ops/digestset.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-06-26 13:18:23 -0400
committerNick Mathewson <nickm@torproject.org>2018-06-26 13:27:23 -0400
commitbf89278c799e718a3b6c1ea139a6069db054ac09 (patch)
treeaddcdf08f01a3aff7645ea42606d2cc7ed5e1a25 /src/lib/crypt_ops/digestset.c
parent860b9a991879c5be2b32cf98766adf5fdd349d41 (diff)
downloadtor-bf89278c799e718a3b6c1ea139a6069db054ac09.tar.gz
tor-bf89278c799e718a3b6c1ea139a6069db054ac09.zip
Refactor bloom filter logic not to be digest-specific.
Now the address-set code and the digest-set code share the same backend. Closes ticket 26510
Diffstat (limited to 'src/lib/crypt_ops/digestset.c')
-rw-r--r--src/lib/crypt_ops/digestset.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/lib/crypt_ops/digestset.c b/src/lib/crypt_ops/digestset.c
new file mode 100644
index 0000000000..89dd377a9c
--- /dev/null
+++ b/src/lib/crypt_ops/digestset.c
@@ -0,0 +1,58 @@
+/* Copyright (c) 2018-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file digestset.c
+ * \brief Implementation for a set of digests
+ **/
+
+#include "orconfig.h"
+#include "lib/container/bloomfilt.h"
+#include "lib/crypt_ops/crypto_rand.h"
+#include "lib/defs/digest_sizes.h"
+#include "lib/crypt_ops/digestset.h"
+#include "siphash.h"
+
+/* Wrap our hash function to have the signature that the bloom filter
+ * needs. */
+static uint64_t
+bloomfilt_digest_hash(const struct sipkey *key,
+ const void *item)
+{
+ return siphash24(item, DIGEST_LEN, key);
+}
+
+/**
+ * Allocate and return an digestset, suitable for holding up to
+ * <b>max_guess</b> distinct values.
+ */
+digestset_t *
+digestset_new(int max_guess)
+{
+ uint8_t k[BLOOMFILT_KEY_LEN];
+ crypto_rand((void*)k, sizeof(k));
+ return bloomfilt_new(max_guess, bloomfilt_digest_hash, k);
+}
+
+/**
+ * Add <b>digest</b> to <b>set</b>.
+ *
+ * All future queries for <b>digest</b> in set will return true. Removing
+ * items is not possible.
+ */
+void
+digestset_add(digestset_t *set, const char *digest)
+{
+ bloomfilt_add(set, digest);
+}
+
+/**
+ * Return true if <b>digest</b> is a member of <b>set</b>. (And probably,
+ * return false if <b>digest</b> is not a member of set.)
+ */
+int
+digestset_probably_contains(const digestset_t *set,
+ const char *digest)
+{
+ return bloomfilt_probably_contains(set, digest);
+}