rehash-crypto-from-hash.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* GNU Guix --- Functional package management for GNU
  2. Copyright © 2020 Maxime Devos <maxime.devos@student.kuleuven.be>
  3. This file is part of GNU Guix.
  4. GNU Guix is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at
  7. your option) any later version.
  8. GNU Guix is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <gnunet/gnunet_config.h>
  15. #include <gnunet/gnunet_crypto_lib.h>
  16. #include <stdint.h>
  17. #include "rehash_crypto.h"
  18. int
  19. GNUNET_REHASH_cleartext_query_from_hash (
  20. enum GNUNET_REHASH_Hash_Type out_type,
  21. enum GNUNET_REHASH_Hash_Type in_type,
  22. const char *data,
  23. size_t data_size,
  24. struct GNUNET_HashCode *query)
  25. {
  26. size_t query_size;
  27. char *query_data;
  28. struct GNUNET_REHASH_Request *header;
  29. char *footer;
  30. if (data_size > 64)
  31. /* Don't allocate much.
  32. There are no hashes of size > 64. */
  33. return GNUNET_NO;
  34. /* Allocation */
  35. query_size = sizeof (struct GNUNET_REHASH_Request) + data_size;
  36. query_data = GNUNET_malloc(query_size);
  37. header = (struct GNUNET_REHASH_Request *) query_data;
  38. footer = query_data + sizeof (struct GNUNET_REHASH_Request);
  39. /* Preparing data structures */
  40. header->key_type = htonl(in_type);
  41. header->value_type = htonl(out_type);
  42. memcpy(footer, data, data_size);
  43. /* Perform! */
  44. GNUNET_CRYPTO_hash(query_data, query_size, query);
  45. /* Deallocation */
  46. GNUNET_free(query_data);
  47. return GNUNET_OK;
  48. }
  49. int
  50. GNUNET_REHASH_obfuscated_query_from_hash (
  51. enum GNUNET_REHASH_Hash_Type out_type,
  52. enum GNUNET_REHASH_Hash_Type in_type,
  53. const char *data,
  54. size_t data_size,
  55. struct GNUNET_HashCode *query)
  56. {
  57. struct GNUNET_HashCode cleartext;
  58. if (GNUNET_OK != GNUNET_REHASH_cleartext_query_from_hash (
  59. out_type, in_type, data, data_size, &cleartext))
  60. return GNUNET_NO;
  61. /* Obfuscated query is used as key in the DHT,
  62. cleartext query is used for encrypting the
  63. data, such that only nodes that know the
  64. original query can parse the resulting data. */
  65. GNUNET_CRYPTO_hash (&cleartext, sizeof(struct GNUNET_HashCode), query);
  66. return GNUNET_OK;
  67. }