diff options
Diffstat (limited to 'nacl/crypto_stream/salsa208/ref/xor.c')
-rw-r--r-- | nacl/crypto_stream/salsa208/ref/xor.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/nacl/crypto_stream/salsa208/ref/xor.c b/nacl/crypto_stream/salsa208/ref/xor.c new file mode 100644 index 00000000..c017ac42 --- /dev/null +++ b/nacl/crypto_stream/salsa208/ref/xor.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | version 20080913 | ||
3 | D. J. Bernstein | ||
4 | Public domain. | ||
5 | */ | ||
6 | |||
7 | #include "crypto_core_salsa208.h" | ||
8 | #include "crypto_stream.h" | ||
9 | |||
10 | typedef unsigned int uint32; | ||
11 | |||
12 | static const unsigned char sigma[16] = "expand 32-byte k"; | ||
13 | |||
14 | int crypto_stream_xor( | ||
15 | unsigned char *c, | ||
16 | const unsigned char *m,unsigned long long mlen, | ||
17 | const unsigned char *n, | ||
18 | const unsigned char *k | ||
19 | ) | ||
20 | { | ||
21 | unsigned char in[16]; | ||
22 | unsigned char block[64]; | ||
23 | int i; | ||
24 | unsigned int u; | ||
25 | |||
26 | if (!mlen) return 0; | ||
27 | |||
28 | for (i = 0;i < 8;++i) in[i] = n[i]; | ||
29 | for (i = 8;i < 16;++i) in[i] = 0; | ||
30 | |||
31 | while (mlen >= 64) { | ||
32 | crypto_core_salsa208(block,in,k,sigma); | ||
33 | for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i]; | ||
34 | |||
35 | u = 1; | ||
36 | for (i = 8;i < 16;++i) { | ||
37 | u += (unsigned int) in[i]; | ||
38 | in[i] = u; | ||
39 | u >>= 8; | ||
40 | } | ||
41 | |||
42 | mlen -= 64; | ||
43 | c += 64; | ||
44 | m += 64; | ||
45 | } | ||
46 | |||
47 | if (mlen) { | ||
48 | crypto_core_salsa208(block,in,k,sigma); | ||
49 | for (i = 0;i < mlen;++i) c[i] = m[i] ^ block[i]; | ||
50 | } | ||
51 | return 0; | ||
52 | } | ||