libsodium: updated to 1.0.10
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_stream / salsa20 / ref / xor_salsa20_ref.c
blobd5984965213aca61a76d77aae1538734f4d96cb8
1 /*
2 version 20140420
3 D. J. Bernstein
4 Public domain.
5 */
7 #include <stdint.h>
9 #include "crypto_core_salsa20.h"
10 #include "crypto_stream_salsa20.h"
11 #include "utils.h"
13 #ifndef HAVE_AMD64_ASM
15 int crypto_stream_salsa20_xor_ic(
16 unsigned char *c,
17 const unsigned char *m,unsigned long long mlen,
18 const unsigned char *n, uint64_t ic,
19 const unsigned char *k
22 unsigned char in[16];
23 unsigned char block[64];
24 unsigned char kcopy[32];
25 unsigned int i;
26 unsigned int u;
28 if (!mlen) return 0;
30 for (i = 0;i < 32;++i) kcopy[i] = k[i];
31 for (i = 0;i < 8;++i) in[i] = n[i];
32 for (i = 8;i < 16;++i) {
33 in[i] = (unsigned char) (ic & 0xff);
34 ic >>= 8;
37 while (mlen >= 64) {
38 crypto_core_salsa20(block,in,kcopy,NULL);
39 for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i];
41 u = 1;
42 for (i = 8;i < 16;++i) {
43 u += (unsigned int) in[i];
44 in[i] = u;
45 u >>= 8;
48 mlen -= 64;
49 c += 64;
50 m += 64;
53 if (mlen) {
54 crypto_core_salsa20(block,in,kcopy,NULL);
55 for (i = 0;i < (unsigned int) mlen;++i) c[i] = m[i] ^ block[i];
57 sodium_memzero(block, sizeof block);
58 sodium_memzero(kcopy, sizeof kcopy);
60 return 0;
63 #endif