libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_stream / salsa208 / ref / xor_salsa208.c
blob9f6dac55d738edc0be1300133e9efd1fdc34a868
1 /*
2 version 20080913
3 D. J. Bernstein
4 Public domain.
5 */
7 #include "api.h"
8 #include "crypto_core_salsa208.h"
10 typedef unsigned int uint32;
12 static const unsigned char sigma[16] = {
13 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
16 int crypto_stream_xor(
17 unsigned char *c,
18 const unsigned char *m,unsigned long long mlen,
19 const unsigned char *n,
20 const unsigned char *k
23 unsigned char in[16];
24 unsigned char block[64];
25 unsigned long long i;
26 unsigned int u;
28 if (!mlen) return 0;
30 for (i = 0;i < 8;++i) in[i] = n[i];
31 for (i = 8;i < 16;++i) in[i] = 0;
33 while (mlen >= 64) {
34 crypto_core_salsa208(block,in,k,sigma);
35 for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i];
37 u = 1;
38 for (i = 8;i < 16;++i) {
39 u += (unsigned int) in[i];
40 in[i] = u;
41 u >>= 8;
44 mlen -= 64;
45 c += 64;
46 m += 64;
49 if (mlen) {
50 crypto_core_salsa208(block,in,k,sigma);
51 for (i = 0;i < mlen;++i) c[i] = m[i] ^ block[i];
53 return 0;