xor.c 963 B

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