digestset.c 1.4 KB

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