libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_stream / salsa20 / ref / xor_salsa20_ref.c
blob7d728b272b7d70585d65cfdcb2b7ad411765d955
1 /*
2 version 20080913
3 D. J. Bernstein
4 Public domain.
5 */
7 #include "api.h"
8 #include "crypto_core_salsa20.h"
10 #ifndef HAVE_AMD64_ASM
12 typedef unsigned int uint32;
14 static const unsigned char sigma[16] = {
15 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
18 int crypto_stream_xor(
19 unsigned char *c,
20 const unsigned char *m,unsigned long long mlen,
21 const unsigned char *n,
22 const unsigned char *k
25 unsigned char in[16];
26 unsigned char block[64];
27 unsigned long long i;
28 unsigned int u;
30 if (!mlen) return 0;
32 for (i = 0;i < 8;++i) in[i] = n[i];
33 for (i = 8;i < 16;++i) in[i] = 0;
35 while (mlen >= 64) {
36 crypto_core_salsa20(block,in,k,sigma);
37 for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i];
39 u = 1;
40 for (i = 8;i < 16;++i) {
41 u += (unsigned int) in[i];
42 in[i] = u;
43 u >>= 8;
46 mlen -= 64;
47 c += 64;
48 m += 64;
51 if (mlen) {
52 crypto_core_salsa20(block,in,k,sigma);
53 for (i = 0;i < mlen;++i) c[i] = m[i] ^ block[i];
55 return 0;
58 #endif