libsodium: updated to 1.0.10
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_stream / salsa208 / ref / xor_salsa208.c
blobfdbd59383de35900c7cf225af176b41044ba59c0
1 /*
2 version 20140420
3 D. J. Bernstein
4 Public domain.
5 */
7 #include "crypto_core_salsa208.h"
8 #include "crypto_stream_salsa208.h"
9 #include "utils.h"
11 int crypto_stream_salsa208_xor(
12 unsigned char *c,
13 const unsigned char *m,unsigned long long mlen,
14 const unsigned char *n,
15 const unsigned char *k
18 unsigned char in[16];
19 unsigned char block[64];
20 unsigned char kcopy[32];
21 unsigned int i;
22 unsigned int u;
24 if (!mlen) return 0;
26 for (i = 0;i < 32;++i) kcopy[i] = k[i];
27 for (i = 0;i < 8;++i) in[i] = n[i];
28 for (i = 8;i < 16;++i) in[i] = 0;
30 while (mlen >= 64) {
31 crypto_core_salsa208(block,in,kcopy,NULL);
32 for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i];
34 u = 1;
35 for (i = 8;i < 16;++i) {
36 u += (unsigned int) in[i];
37 in[i] = u;
38 u >>= 8;
41 mlen -= 64;
42 c += 64;
43 m += 64;
46 if (mlen) {
47 crypto_core_salsa208(block,in,kcopy,NULL);
48 for (i = 0;i < (unsigned int) mlen;++i) c[i] = m[i] ^ block[i];
50 sodium_memzero(block, sizeof block);
51 sodium_memzero(kcopy, sizeof kcopy);
53 return 0;