rehash-crypto-from-data.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_mapping_for_data (
  20. enum GNUNET_REHASH_Hash_Type out_type,
  21. enum GNUNET_REHASH_Hash_Type in_type,
  22. const char *in_data,
  23. size_t in_size,
  24. const char *dht_data,
  25. size_t dht_size,
  26. char *cleartext)
  27. {
  28. struct GNUNET_HashCode clear_query;
  29. struct GNUNET_CRYPTO_SymmetricSessionKey skey;
  30. struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
  31. /* Buffer size (and overflow prevention) check.
  32. In practice, hash length is rather limited.
  33. Future hashes may require more bytes. */
  34. if (dht_size > 64)
  35. return GNUNET_NO;
  36. /* Derive an encryption key from cleartext query */
  37. if (GNUNET_OK != GNUNET_REHASH_cleartext_query_from_hash
  38. (out_type, in_type, in_data, in_size, &clear_query))
  39. return GNUNET_NO;
  40. GNUNET_CRYPTO_hash_to_aes_key (&clear_query, &skey, &iv);
  41. /* Seems OK, decrypt */
  42. if ((ssize_t) dht_size != GNUNET_CRYPTO_symmetric_decrypt
  43. (dht_data, dht_size, &skey, &iv, cleartext))
  44. return GNUNET_NO;
  45. return GNUNET_OK;
  46. }