summaryrefslogtreecommitdiff
path: root/nacl/crypto_stream/salsa2012/ref
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-02 09:53:34 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-02 09:53:34 -0400
commite2967396ac73cb7410787886cdaf072a184ffc49 (patch)
tree527a74d25a4a0705fc641994fd35bfab22662034 /nacl/crypto_stream/salsa2012/ref
parent8928c817df345f29aa0b194743595aa11bd6a8ba (diff)
Added NaCl crypto library.
Diffstat (limited to 'nacl/crypto_stream/salsa2012/ref')
-rw-r--r--nacl/crypto_stream/salsa2012/ref/api.h2
-rw-r--r--nacl/crypto_stream/salsa2012/ref/implementors1
-rw-r--r--nacl/crypto_stream/salsa2012/ref/stream.c49
-rw-r--r--nacl/crypto_stream/salsa2012/ref/xor.c52
4 files changed, 104 insertions, 0 deletions
diff --git a/nacl/crypto_stream/salsa2012/ref/api.h b/nacl/crypto_stream/salsa2012/ref/api.h
new file mode 100644
index 00000000..c2b18461
--- /dev/null
+++ b/nacl/crypto_stream/salsa2012/ref/api.h
@@ -0,0 +1,2 @@
1#define CRYPTO_KEYBYTES 32
2#define CRYPTO_NONCEBYTES 8
diff --git a/nacl/crypto_stream/salsa2012/ref/implementors b/nacl/crypto_stream/salsa2012/ref/implementors
new file mode 100644
index 00000000..f6fb3c73
--- /dev/null
+++ b/nacl/crypto_stream/salsa2012/ref/implementors
@@ -0,0 +1 @@
Daniel J. Bernstein
diff --git a/nacl/crypto_stream/salsa2012/ref/stream.c b/nacl/crypto_stream/salsa2012/ref/stream.c
new file mode 100644
index 00000000..86053337
--- /dev/null
+++ b/nacl/crypto_stream/salsa2012/ref/stream.c
@@ -0,0 +1,49 @@
1/*
2version 20080913
3D. J. Bernstein
4Public domain.
5*/
6
7#include "crypto_core_salsa2012.h"
8#include "crypto_stream.h"
9
10typedef unsigned int uint32;
11
12static const unsigned char sigma[16] = "expand 32-byte k";
13
14int crypto_stream(
15 unsigned char *c,unsigned long long clen,
16 const unsigned char *n,
17 const unsigned char *k
18)
19{
20 unsigned char in[16];
21 unsigned char block[64];
22 int i;
23 unsigned int u;
24
25 if (!clen) return 0;
26
27 for (i = 0;i < 8;++i) in[i] = n[i];
28 for (i = 8;i < 16;++i) in[i] = 0;
29
30 while (clen >= 64) {
31 crypto_core_salsa2012(c,in,k,sigma);
32
33 u = 1;
34 for (i = 8;i < 16;++i) {
35 u += (unsigned int) in[i];
36 in[i] = u;
37 u >>= 8;
38 }
39
40 clen -= 64;
41 c += 64;
42 }
43
44 if (clen) {
45 crypto_core_salsa2012(block,in,k,sigma);
46 for (i = 0;i < clen;++i) c[i] = block[i];
47 }
48 return 0;
49}
diff --git a/nacl/crypto_stream/salsa2012/ref/xor.c b/nacl/crypto_stream/salsa2012/ref/xor.c
new file mode 100644
index 00000000..90206426
--- /dev/null
+++ b/nacl/crypto_stream/salsa2012/ref/xor.c
@@ -0,0 +1,52 @@
1/*
2version 20080913
3D. J. Bernstein
4Public domain.
5*/
6
7#include "crypto_core_salsa2012.h"
8#include "crypto_stream.h"
9
10typedef unsigned int uint32;
11
12static const unsigned char sigma[16] = "expand 32-byte k";
13
14int 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_salsa2012(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_salsa2012(block,in,k,sigma);
49 for (i = 0;i < mlen;++i) c[i] = m[i] ^ block[i];
50 }
51 return 0;
52}