tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

digestset.c (1431B)


      1 /* Copyright (c) 2018-2021, The Tor Project, Inc. */
      2 /* See LICENSE for licensing information */
      3 
      4 /**
      5 * \file digestset.c
      6 * \brief Implementation for a set of digests
      7 **/
      8 
      9 #include "orconfig.h"
     10 #include "lib/container/bloomfilt.h"
     11 #include "lib/crypt_ops/crypto_rand.h"
     12 #include "lib/defs/digest_sizes.h"
     13 #include "lib/crypt_ops/digestset.h"
     14 #include "ext/siphash.h"
     15 
     16 /* Wrap our hash function to have the signature that the bloom filter
     17 * needs. */
     18 static uint64_t
     19 bloomfilt_digest_hash(const struct sipkey *key,
     20                      const void *item)
     21 {
     22  return siphash24(item, DIGEST_LEN, key);
     23 }
     24 
     25 /**
     26 * Allocate and return an digestset, suitable for holding up to
     27 * <b>max_guess</b> distinct values.
     28 */
     29 digestset_t *
     30 digestset_new(int max_guess)
     31 {
     32  uint8_t k[BLOOMFILT_KEY_LEN];
     33  crypto_rand((void*)k, sizeof(k));
     34  return bloomfilt_new(max_guess, bloomfilt_digest_hash, k);
     35 }
     36 
     37 /**
     38 * Add <b>digest</b> to <b>set</b>.
     39 *
     40 * All future queries for <b>digest</b> in set will return true. Removing
     41 * items is not possible.
     42 */
     43 void
     44 digestset_add(digestset_t *set, const char *digest)
     45 {
     46  bloomfilt_add(set, digest);
     47 }
     48 
     49 /**
     50 * Return true if <b>digest</b> is a member of <b>set</b>.  (And probably,
     51 * return false if <b>digest</b> is not a member of set.)
     52 */
     53 int
     54 digestset_probably_contains(const digestset_t *set,
     55                            const char *digest)
     56 {
     57  return bloomfilt_probably_contains(set, digest);
     58 }