stream_xsalsa20.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "crypto_core_hsalsa20.h"
  2. #include "crypto_stream_salsa20.h"
  3. #include "crypto_stream_xsalsa20.h"
  4. #include "randombytes.h"
  5. #include "utils.h"
  6. int
  7. crypto_stream_xsalsa20(unsigned char *c, unsigned long long clen,
  8. const unsigned char *n, const unsigned char *k)
  9. {
  10. unsigned char subkey[32];
  11. int ret;
  12. crypto_core_hsalsa20(subkey, n, k, NULL);
  13. ret = crypto_stream_salsa20(c, clen, n + 16, subkey);
  14. sodium_memzero(subkey, sizeof subkey);
  15. return ret;
  16. }
  17. int
  18. crypto_stream_xsalsa20_xor_ic(unsigned char *c, const unsigned char *m,
  19. unsigned long long mlen, const unsigned char *n,
  20. uint64_t ic, const unsigned char *k)
  21. {
  22. unsigned char subkey[32];
  23. int ret;
  24. crypto_core_hsalsa20(subkey, n, k, NULL);
  25. ret = crypto_stream_salsa20_xor_ic(c, m, mlen, n + 16, ic, subkey);
  26. sodium_memzero(subkey, sizeof subkey);
  27. return ret;
  28. }
  29. int
  30. crypto_stream_xsalsa20_xor(unsigned char *c, const unsigned char *m,
  31. unsigned long long mlen, const unsigned char *n,
  32. const unsigned char *k)
  33. {
  34. return crypto_stream_xsalsa20_xor_ic(c, m, mlen, n, 0ULL, k);
  35. }
  36. size_t
  37. crypto_stream_xsalsa20_keybytes(void)
  38. {
  39. return crypto_stream_xsalsa20_KEYBYTES;
  40. }
  41. size_t
  42. crypto_stream_xsalsa20_noncebytes(void)
  43. {
  44. return crypto_stream_xsalsa20_NONCEBYTES;
  45. }
  46. size_t
  47. crypto_stream_xsalsa20_messagebytes_max(void)
  48. {
  49. return crypto_stream_xsalsa20_MESSAGEBYTES_MAX;
  50. }
  51. void
  52. crypto_stream_xsalsa20_keygen(unsigned char k[crypto_stream_xsalsa20_KEYBYTES])
  53. {
  54. randombytes_buf(k, crypto_stream_xsalsa20_KEYBYTES);
  55. }