stream.c 844 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. version 20080913
  3. D. J. Bernstein
  4. Public domain.
  5. */
  6. #include "crypto_core_salsa2012.h"
  7. #include "crypto_stream.h"
  8. typedef unsigned int uint32;
  9. static const unsigned char sigma[16] = "expand 32-byte k";
  10. int crypto_stream(
  11. unsigned char *c,unsigned long long clen,
  12. const unsigned char *n,
  13. const unsigned char *k
  14. )
  15. {
  16. unsigned char in[16];
  17. unsigned char block[64];
  18. int i;
  19. unsigned int u;
  20. if (!clen) return 0;
  21. for (i = 0;i < 8;++i) in[i] = n[i];
  22. for (i = 8;i < 16;++i) in[i] = 0;
  23. while (clen >= 64) {
  24. crypto_core_salsa2012(c,in,k,sigma);
  25. u = 1;
  26. for (i = 8;i < 16;++i) {
  27. u += (unsigned int) in[i];
  28. in[i] = u;
  29. u >>= 8;
  30. }
  31. clen -= 64;
  32. c += 64;
  33. }
  34. if (clen) {
  35. crypto_core_salsa2012(block,in,k,sigma);
  36. for (i = 0;i < clen;++i) c[i] = block[i];
  37. }
  38. return 0;
  39. }