rehash-crypto-to-data.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. ssize_t
  19. GNUNET_REHASH_data_for_mapping (
  20. enum GNUNET_REHASH_Hash_Type out_type,
  21. enum GNUNET_REHASH_Hash_Type in_type,
  22. const char *out_data,
  23. const char *in_data,
  24. size_t out_size,
  25. size_t in_size,
  26. char *dest,
  27. size_t dest_size)
  28. {
  29. struct GNUNET_HashCode clear_query;
  30. struct GNUNET_CRYPTO_SymmetricSessionKey skey;
  31. struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
  32. /* Buffer size (and overflow prevention) check*/
  33. if (out_size > 64)
  34. return -1;
  35. if (dest_size < out_size)
  36. return -1;
  37. /* Derive an encryption key from cleartext query */
  38. if (GNUNET_OK != GNUNET_REHASH_cleartext_query_from_hash
  39. (out_type, in_type, in_data, in_size, &clear_query))
  40. return -1;
  41. GNUNET_CRYPTO_hash_to_aes_key (&clear_query, &skey, &iv);
  42. /* Encrypt @a out_data with skey and iv */
  43. if (out_size != GNUNET_CRYPTO_symmetric_encrypt
  44. (out_data, out_size, &skey, &iv, dest))
  45. return -1;
  46. return out_size;
  47. }