libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_stream / salsa2012 / ref / stream_salsa2012.c
blob793adaa570ac1a9bd84a1b4cdff89fb63bf643a2
1 /*
2 version 20080913
3 D. J. Bernstein
4 Public domain.
5 */
7 #include "api.h"
8 #include "crypto_core_salsa2012.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(
17 unsigned char *c,unsigned long long clen,
18 const unsigned char *n,
19 const unsigned char *k
22 unsigned char in[16];
23 unsigned char block[64];
24 unsigned long long i;
25 unsigned int u;
27 if (!clen) return 0;
29 for (i = 0;i < 8;++i) in[i] = n[i];
30 for (i = 8;i < 16;++i) in[i] = 0;
32 while (clen >= 64) {
33 crypto_core_salsa2012(c,in,k,sigma);
35 u = 1;
36 for (i = 8;i < 16;++i) {
37 u += (unsigned int) in[i];
38 in[i] = u;
39 u >>= 8;
42 clen -= 64;
43 c += 64;
46 if (clen) {
47 crypto_core_salsa2012(block,in,k,sigma);
48 for (i = 0;i < clen;++i) c[i] = block[i];
50 return 0;