diff options
Diffstat (limited to 'nacl/crypto_box')
-rw-r--r-- | nacl/crypto_box/curve25519xsalsa20poly1305/checksum | 1 | ||||
-rw-r--r-- | nacl/crypto_box/curve25519xsalsa20poly1305/ref/after.c | 22 | ||||
-rw-r--r-- | nacl/crypto_box/curve25519xsalsa20poly1305/ref/api.h | 6 | ||||
-rw-r--r-- | nacl/crypto_box/curve25519xsalsa20poly1305/ref/before.c | 17 | ||||
-rw-r--r-- | nacl/crypto_box/curve25519xsalsa20poly1305/ref/box.c | 27 | ||||
-rw-r--r-- | nacl/crypto_box/curve25519xsalsa20poly1305/ref/keypair.c | 12 | ||||
-rw-r--r-- | nacl/crypto_box/curve25519xsalsa20poly1305/selected | 0 | ||||
-rw-r--r-- | nacl/crypto_box/curve25519xsalsa20poly1305/used | 0 | ||||
-rw-r--r-- | nacl/crypto_box/measure.c | 137 | ||||
-rw-r--r-- | nacl/crypto_box/try.c | 195 | ||||
-rw-r--r-- | nacl/crypto_box/wrapper-box.cpp | 24 | ||||
-rw-r--r-- | nacl/crypto_box/wrapper-keypair.cpp | 12 | ||||
-rw-r--r-- | nacl/crypto_box/wrapper-open.cpp | 27 |
13 files changed, 480 insertions, 0 deletions
diff --git a/nacl/crypto_box/curve25519xsalsa20poly1305/checksum b/nacl/crypto_box/curve25519xsalsa20poly1305/checksum new file mode 100644 index 00000000..56a20083 --- /dev/null +++ b/nacl/crypto_box/curve25519xsalsa20poly1305/checksum | |||
@@ -0,0 +1 @@ | |||
5fac7400caabc14a99c5c0bc13fb1df5e468e870382a3a1c | |||
diff --git a/nacl/crypto_box/curve25519xsalsa20poly1305/ref/after.c b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/after.c new file mode 100644 index 00000000..eb243e22 --- /dev/null +++ b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/after.c | |||
@@ -0,0 +1,22 @@ | |||
1 | #include "crypto_secretbox_xsalsa20poly1305.h" | ||
2 | #include "crypto_box.h" | ||
3 | |||
4 | int crypto_box_afternm( | ||
5 | unsigned char *c, | ||
6 | const unsigned char *m,unsigned long long mlen, | ||
7 | const unsigned char *n, | ||
8 | const unsigned char *k | ||
9 | ) | ||
10 | { | ||
11 | return crypto_secretbox_xsalsa20poly1305(c,m,mlen,n,k); | ||
12 | } | ||
13 | |||
14 | int crypto_box_open_afternm( | ||
15 | unsigned char *m, | ||
16 | const unsigned char *c,unsigned long long clen, | ||
17 | const unsigned char *n, | ||
18 | const unsigned char *k | ||
19 | ) | ||
20 | { | ||
21 | return crypto_secretbox_xsalsa20poly1305_open(m,c,clen,n,k); | ||
22 | } | ||
diff --git a/nacl/crypto_box/curve25519xsalsa20poly1305/ref/api.h b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/api.h new file mode 100644 index 00000000..ce7762df --- /dev/null +++ b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/api.h | |||
@@ -0,0 +1,6 @@ | |||
1 | #define CRYPTO_PUBLICKEYBYTES 32 | ||
2 | #define CRYPTO_SECRETKEYBYTES 32 | ||
3 | #define CRYPTO_BEFORENMBYTES 32 | ||
4 | #define CRYPTO_NONCEBYTES 24 | ||
5 | #define CRYPTO_ZEROBYTES 32 | ||
6 | #define CRYPTO_BOXZEROBYTES 16 | ||
diff --git a/nacl/crypto_box/curve25519xsalsa20poly1305/ref/before.c b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/before.c new file mode 100644 index 00000000..279bb12a --- /dev/null +++ b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/before.c | |||
@@ -0,0 +1,17 @@ | |||
1 | #include "crypto_core_hsalsa20.h" | ||
2 | #include "crypto_scalarmult_curve25519.h" | ||
3 | #include "crypto_box.h" | ||
4 | |||
5 | static const unsigned char sigma[16] = "expand 32-byte k"; | ||
6 | static const unsigned char n[16] = {0}; | ||
7 | |||
8 | int crypto_box_beforenm( | ||
9 | unsigned char *k, | ||
10 | const unsigned char *pk, | ||
11 | const unsigned char *sk | ||
12 | ) | ||
13 | { | ||
14 | unsigned char s[32]; | ||
15 | crypto_scalarmult_curve25519(s,sk,pk); | ||
16 | return crypto_core_hsalsa20(k,n,s,sigma); | ||
17 | } | ||
diff --git a/nacl/crypto_box/curve25519xsalsa20poly1305/ref/box.c b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/box.c new file mode 100644 index 00000000..81ff72e2 --- /dev/null +++ b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/box.c | |||
@@ -0,0 +1,27 @@ | |||
1 | #include "crypto_box.h" | ||
2 | |||
3 | int crypto_box( | ||
4 | unsigned char *c, | ||
5 | const unsigned char *m,unsigned long long mlen, | ||
6 | const unsigned char *n, | ||
7 | const unsigned char *pk, | ||
8 | const unsigned char *sk | ||
9 | ) | ||
10 | { | ||
11 | unsigned char k[crypto_box_BEFORENMBYTES]; | ||
12 | crypto_box_beforenm(k,pk,sk); | ||
13 | return crypto_box_afternm(c,m,mlen,n,k); | ||
14 | } | ||
15 | |||
16 | int crypto_box_open( | ||
17 | unsigned char *m, | ||
18 | const unsigned char *c,unsigned long long clen, | ||
19 | const unsigned char *n, | ||
20 | const unsigned char *pk, | ||
21 | const unsigned char *sk | ||
22 | ) | ||
23 | { | ||
24 | unsigned char k[crypto_box_BEFORENMBYTES]; | ||
25 | crypto_box_beforenm(k,pk,sk); | ||
26 | return crypto_box_open_afternm(m,c,clen,n,k); | ||
27 | } | ||
diff --git a/nacl/crypto_box/curve25519xsalsa20poly1305/ref/keypair.c b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/keypair.c new file mode 100644 index 00000000..233bc950 --- /dev/null +++ b/nacl/crypto_box/curve25519xsalsa20poly1305/ref/keypair.c | |||
@@ -0,0 +1,12 @@ | |||
1 | #include "crypto_scalarmult_curve25519.h" | ||
2 | #include "crypto_box.h" | ||
3 | #include "randombytes.h" | ||
4 | |||
5 | int crypto_box_keypair( | ||
6 | unsigned char *pk, | ||
7 | unsigned char *sk | ||
8 | ) | ||
9 | { | ||
10 | randombytes(sk,32); | ||
11 | return crypto_scalarmult_curve25519_base(pk,sk); | ||
12 | } | ||
diff --git a/nacl/crypto_box/curve25519xsalsa20poly1305/selected b/nacl/crypto_box/curve25519xsalsa20poly1305/selected new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/nacl/crypto_box/curve25519xsalsa20poly1305/selected | |||
diff --git a/nacl/crypto_box/curve25519xsalsa20poly1305/used b/nacl/crypto_box/curve25519xsalsa20poly1305/used new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/nacl/crypto_box/curve25519xsalsa20poly1305/used | |||
diff --git a/nacl/crypto_box/measure.c b/nacl/crypto_box/measure.c new file mode 100644 index 00000000..08df1e39 --- /dev/null +++ b/nacl/crypto_box/measure.c | |||
@@ -0,0 +1,137 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include "randombytes.h" | ||
3 | #include "cpucycles.h" | ||
4 | #include "crypto_box.h" | ||
5 | |||
6 | extern void printentry(long long,const char *,long long *,long long); | ||
7 | extern unsigned char *alignedcalloc(unsigned long long); | ||
8 | extern const char *primitiveimplementation; | ||
9 | extern const char *implementationversion; | ||
10 | extern const char *sizenames[]; | ||
11 | extern const long long sizes[]; | ||
12 | extern void allocate(void); | ||
13 | extern void measure(void); | ||
14 | |||
15 | const char *primitiveimplementation = crypto_box_IMPLEMENTATION; | ||
16 | const char *implementationversion = crypto_box_VERSION; | ||
17 | const char *sizenames[] = { "publickeybytes", "secretkeybytes", "beforenmbytes", "noncebytes", "zerobytes", "boxzerobytes", 0 }; | ||
18 | const long long sizes[] = { crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES, crypto_box_BEFORENMBYTES, crypto_box_NONCEBYTES, crypto_box_ZEROBYTES, crypto_box_BOXZEROBYTES }; | ||
19 | |||
20 | #define MAXTEST_BYTES 4096 | ||
21 | |||
22 | static unsigned char *ska; | ||
23 | static unsigned char *pka; | ||
24 | static unsigned char *skb; | ||
25 | static unsigned char *pkb; | ||
26 | static unsigned char *n; | ||
27 | static unsigned char *m; | ||
28 | static unsigned char *c; | ||
29 | static unsigned char *sa; | ||
30 | static unsigned char *sb; | ||
31 | |||
32 | void preallocate(void) | ||
33 | { | ||
34 | } | ||
35 | |||
36 | void allocate(void) | ||
37 | { | ||
38 | ska = alignedcalloc(crypto_box_SECRETKEYBYTES); | ||
39 | pka = alignedcalloc(crypto_box_PUBLICKEYBYTES); | ||
40 | skb = alignedcalloc(crypto_box_SECRETKEYBYTES); | ||
41 | pkb = alignedcalloc(crypto_box_PUBLICKEYBYTES); | ||
42 | n = alignedcalloc(crypto_box_NONCEBYTES); | ||
43 | m = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
44 | c = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
45 | sa = alignedcalloc(crypto_box_BEFORENMBYTES); | ||
46 | sb = alignedcalloc(crypto_box_BEFORENMBYTES); | ||
47 | } | ||
48 | |||
49 | #define TIMINGS 15 | ||
50 | static long long cycles[TIMINGS + 1]; | ||
51 | |||
52 | void measure(void) | ||
53 | { | ||
54 | int i; | ||
55 | int loop; | ||
56 | int mlen; | ||
57 | |||
58 | for (loop = 0;loop < LOOPS;++loop) { | ||
59 | for (i = 0;i <= TIMINGS;++i) { | ||
60 | cycles[i] = cpucycles(); | ||
61 | crypto_box_keypair(pka,ska); | ||
62 | } | ||
63 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
64 | printentry(-1,"keypair_cycles",cycles,TIMINGS); | ||
65 | |||
66 | for (i = 0;i <= TIMINGS;++i) { | ||
67 | cycles[i] = cpucycles(); | ||
68 | crypto_box_keypair(pkb,skb); | ||
69 | } | ||
70 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
71 | printentry(-1,"keypair_cycles",cycles,TIMINGS); | ||
72 | |||
73 | for (i = 0;i <= TIMINGS;++i) { | ||
74 | cycles[i] = cpucycles(); | ||
75 | crypto_box_beforenm(sa,pkb,ska); | ||
76 | } | ||
77 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
78 | printentry(-1,"beforenm_cycles",cycles,TIMINGS); | ||
79 | |||
80 | for (i = 0;i <= TIMINGS;++i) { | ||
81 | cycles[i] = cpucycles(); | ||
82 | crypto_box_beforenm(sb,pka,skb); | ||
83 | } | ||
84 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
85 | printentry(-1,"beforenm_cycles",cycles,TIMINGS); | ||
86 | |||
87 | for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / 8) { | ||
88 | randombytes(n,crypto_box_NONCEBYTES); | ||
89 | randombytes(m + crypto_box_ZEROBYTES,mlen); | ||
90 | randombytes(c,mlen + crypto_box_ZEROBYTES); | ||
91 | |||
92 | for (i = 0;i <= TIMINGS;++i) { | ||
93 | cycles[i] = cpucycles(); | ||
94 | crypto_box(c,m,mlen + crypto_box_ZEROBYTES,n,pka,skb); | ||
95 | } | ||
96 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
97 | printentry(mlen,"cycles",cycles,TIMINGS); | ||
98 | |||
99 | for (i = 0;i <= TIMINGS;++i) { | ||
100 | cycles[i] = cpucycles(); | ||
101 | crypto_box_open(m,c,mlen + crypto_box_ZEROBYTES,n,pkb,ska); | ||
102 | } | ||
103 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
104 | printentry(mlen,"open_cycles",cycles,TIMINGS); | ||
105 | |||
106 | ++c[crypto_box_ZEROBYTES]; | ||
107 | for (i = 0;i <= TIMINGS;++i) { | ||
108 | cycles[i] = cpucycles(); | ||
109 | crypto_box_open(m,c,mlen + crypto_box_ZEROBYTES,n,pkb,ska); | ||
110 | } | ||
111 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
112 | printentry(mlen,"forgery_open_cycles",cycles,TIMINGS); | ||
113 | |||
114 | for (i = 0;i <= TIMINGS;++i) { | ||
115 | cycles[i] = cpucycles(); | ||
116 | crypto_box_afternm(c,m,mlen + crypto_box_ZEROBYTES,n,sb); | ||
117 | } | ||
118 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
119 | printentry(mlen,"afternm_cycles",cycles,TIMINGS); | ||
120 | |||
121 | for (i = 0;i <= TIMINGS;++i) { | ||
122 | cycles[i] = cpucycles(); | ||
123 | crypto_box_open_afternm(m,c,mlen + crypto_box_ZEROBYTES,n,sa); | ||
124 | } | ||
125 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
126 | printentry(mlen,"open_afternm_cycles",cycles,TIMINGS); | ||
127 | |||
128 | ++c[crypto_box_ZEROBYTES]; | ||
129 | for (i = 0;i <= TIMINGS;++i) { | ||
130 | cycles[i] = cpucycles(); | ||
131 | crypto_box_open_afternm(m,c,mlen + crypto_box_ZEROBYTES,n,sa); | ||
132 | } | ||
133 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
134 | printentry(mlen,"forgery_open_afternm_cycles",cycles,TIMINGS); | ||
135 | } | ||
136 | } | ||
137 | } | ||
diff --git a/nacl/crypto_box/try.c b/nacl/crypto_box/try.c new file mode 100644 index 00000000..f7029909 --- /dev/null +++ b/nacl/crypto_box/try.c | |||
@@ -0,0 +1,195 @@ | |||
1 | /* | ||
2 | * crypto_box/try.c version 20090118 | ||
3 | * D. J. Bernstein | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include "crypto_box.h" | ||
8 | |||
9 | extern unsigned char *alignedcalloc(unsigned long long); | ||
10 | |||
11 | const char *primitiveimplementation = crypto_box_IMPLEMENTATION; | ||
12 | |||
13 | #define MAXTEST_BYTES 10000 | ||
14 | #define CHECKSUM_BYTES 4096 | ||
15 | #define TUNE_BYTES 1536 | ||
16 | |||
17 | static unsigned char *ska; | ||
18 | static unsigned char *pka; | ||
19 | static unsigned char *skb; | ||
20 | static unsigned char *pkb; | ||
21 | static unsigned char *s; | ||
22 | static unsigned char *n; | ||
23 | static unsigned char *m; | ||
24 | static unsigned char *c; | ||
25 | static unsigned char *t; | ||
26 | static unsigned char *ska2; | ||
27 | static unsigned char *pka2; | ||
28 | static unsigned char *skb2; | ||
29 | static unsigned char *pkb2; | ||
30 | static unsigned char *s2; | ||
31 | static unsigned char *n2; | ||
32 | static unsigned char *m2; | ||
33 | static unsigned char *c2; | ||
34 | static unsigned char *t2; | ||
35 | |||
36 | #define sklen crypto_box_SECRETKEYBYTES | ||
37 | #define pklen crypto_box_PUBLICKEYBYTES | ||
38 | #define nlen crypto_box_NONCEBYTES | ||
39 | #define slen crypto_box_BEFORENMBYTES | ||
40 | |||
41 | void preallocate(void) | ||
42 | { | ||
43 | } | ||
44 | |||
45 | void allocate(void) | ||
46 | { | ||
47 | ska = alignedcalloc(sklen); | ||
48 | pka = alignedcalloc(pklen); | ||
49 | skb = alignedcalloc(sklen); | ||
50 | pkb = alignedcalloc(pklen); | ||
51 | n = alignedcalloc(nlen); | ||
52 | m = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
53 | c = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
54 | t = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
55 | s = alignedcalloc(slen); | ||
56 | ska2 = alignedcalloc(sklen); | ||
57 | pka2 = alignedcalloc(pklen); | ||
58 | skb2 = alignedcalloc(sklen); | ||
59 | pkb2 = alignedcalloc(pklen); | ||
60 | n2 = alignedcalloc(nlen); | ||
61 | m2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
62 | c2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
63 | t2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
64 | s2 = alignedcalloc(slen); | ||
65 | } | ||
66 | |||
67 | void predoit(void) | ||
68 | { | ||
69 | } | ||
70 | |||
71 | void doit(void) | ||
72 | { | ||
73 | crypto_box(c,m,TUNE_BYTES + crypto_box_ZEROBYTES,n,pka,skb); | ||
74 | crypto_box_open(t,c,TUNE_BYTES + crypto_box_ZEROBYTES,n,pkb,ska); | ||
75 | } | ||
76 | |||
77 | char checksum[nlen * 2 + 1]; | ||
78 | |||
79 | const char *checksum_compute(void) | ||
80 | { | ||
81 | long long i; | ||
82 | long long j; | ||
83 | |||
84 | if (crypto_box_keypair(pka,ska) != 0) return "crypto_box_keypair returns nonzero"; | ||
85 | if (crypto_box_keypair(pkb,skb) != 0) return "crypto_box_keypair returns nonzero"; | ||
86 | |||
87 | for (j = 0;j < crypto_box_ZEROBYTES;++j) m[j] = 0; | ||
88 | |||
89 | for (i = 0;i < CHECKSUM_BYTES;++i) { | ||
90 | long long mlen = i + crypto_box_ZEROBYTES; | ||
91 | long long tlen = i + crypto_box_ZEROBYTES; | ||
92 | long long clen = i + crypto_box_ZEROBYTES; | ||
93 | |||
94 | for (j = -16;j < 0;++j) ska[j] = random(); | ||
95 | for (j = -16;j < 0;++j) skb[j] = random(); | ||
96 | for (j = -16;j < 0;++j) pka[j] = random(); | ||
97 | for (j = -16;j < 0;++j) pkb[j] = random(); | ||
98 | for (j = -16;j < 0;++j) m[j] = random(); | ||
99 | for (j = -16;j < 0;++j) n[j] = random(); | ||
100 | |||
101 | for (j = sklen;j < sklen + 16;++j) ska[j] = random(); | ||
102 | for (j = sklen;j < sklen + 16;++j) skb[j] = random(); | ||
103 | for (j = pklen;j < pklen + 16;++j) pka[j] = random(); | ||
104 | for (j = pklen;j < pklen + 16;++j) pkb[j] = random(); | ||
105 | for (j = mlen;j < mlen + 16;++j) m[j] = random(); | ||
106 | for (j = nlen;j < nlen + 16;++j) n[j] = random(); | ||
107 | |||
108 | for (j = -16;j < sklen + 16;++j) ska2[j] = ska[j]; | ||
109 | for (j = -16;j < sklen + 16;++j) skb2[j] = skb[j]; | ||
110 | for (j = -16;j < pklen + 16;++j) pka2[j] = pka[j]; | ||
111 | for (j = -16;j < pklen + 16;++j) pkb2[j] = pkb[j]; | ||
112 | for (j = -16;j < mlen + 16;++j) m2[j] = m[j]; | ||
113 | for (j = -16;j < nlen + 16;++j) n2[j] = n[j]; | ||
114 | for (j = -16;j < clen + 16;++j) c2[j] = c[j] = random(); | ||
115 | |||
116 | if (crypto_box(c,m,mlen,n,pkb,ska) != 0) return "crypto_box returns nonzero"; | ||
117 | |||
118 | for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box overwrites m"; | ||
119 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box overwrites n"; | ||
120 | for (j = -16;j < 0;++j) if (c2[j] != c[j]) return "crypto_box writes before output"; | ||
121 | for (j = clen;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box writes after output"; | ||
122 | for (j = 0;j < crypto_box_BOXZEROBYTES;++j) | ||
123 | if (c[j] != 0) return "crypto_box does not clear extra bytes"; | ||
124 | |||
125 | for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box overwrites ska"; | ||
126 | for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box overwrites skb"; | ||
127 | for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box overwrites pka"; | ||
128 | for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box overwrites pkb"; | ||
129 | |||
130 | for (j = -16;j < 0;++j) c[j] = random(); | ||
131 | for (j = clen;j < clen + 16;++j) c[j] = random(); | ||
132 | for (j = -16;j < clen + 16;++j) c2[j] = c[j]; | ||
133 | for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random(); | ||
134 | |||
135 | if (crypto_box_open(t,c,clen,n,pka,skb) != 0) return "crypto_box_open returns nonzero"; | ||
136 | |||
137 | for (j = -16;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box_open overwrites c"; | ||
138 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open overwrites n"; | ||
139 | for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open writes before output"; | ||
140 | for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open writes after output"; | ||
141 | for (j = 0;j < crypto_box_ZEROBYTES;++j) | ||
142 | if (t[j] != 0) return "crypto_box_open does not clear extra bytes"; | ||
143 | |||
144 | for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box_open overwrites ska"; | ||
145 | for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites skb"; | ||
146 | for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pka"; | ||
147 | for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box_open overwrites pkb"; | ||
148 | |||
149 | for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "plaintext does not match"; | ||
150 | |||
151 | for (j = -16;j < slen + 16;++j) s2[j] = s[j] = random(); | ||
152 | if (crypto_box_beforenm(s,pkb,ska) != 0) return "crypto_box_beforenm returns nonzero"; | ||
153 | for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pk"; | ||
154 | for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites sk"; | ||
155 | for (j = -16;j < 0;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes before output"; | ||
156 | for (j = slen;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes after output"; | ||
157 | |||
158 | for (j = -16;j < slen + 16;++j) s2[j] = s[j]; | ||
159 | for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random(); | ||
160 | if (crypto_box_afternm(t,m,mlen,n,s) != 0) return "crypto_box_afternm returns nonzero"; | ||
161 | for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_afternm overwrites s"; | ||
162 | for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_afternm overwrites m"; | ||
163 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_afternm overwrites n"; | ||
164 | for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes before output"; | ||
165 | for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes after output"; | ||
166 | for (j = 0;j < crypto_box_BOXZEROBYTES;++j) | ||
167 | if (t[j] != 0) return "crypto_box_afternm does not clear extra bytes"; | ||
168 | for (j = 0;j < mlen;++j) if (t[j] != c[j]) return "crypto_box_afternm does not match crypto_box"; | ||
169 | |||
170 | if (crypto_box_beforenm(s,pka,skb) != 0) return "crypto_box_beforenm returns nonzero"; | ||
171 | |||
172 | for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random(); | ||
173 | if (crypto_box_open_afternm(t,c,clen,n,s) != 0) return "crypto_box_open_afternm returns nonzero"; | ||
174 | for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_open_afternm overwrites s"; | ||
175 | for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_open_afternm overwrites m"; | ||
176 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open_afternm overwrites n"; | ||
177 | for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes before output"; | ||
178 | for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes after output"; | ||
179 | for (j = 0;j < crypto_box_ZEROBYTES;++j) | ||
180 | if (t[j] != 0) return "crypto_box_open_afternm does not clear extra bytes"; | ||
181 | for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "crypto_box_open_afternm does not match crypto_box_open"; | ||
182 | |||
183 | for (j = 0;j < i;++j) n[j % nlen] ^= c[j + crypto_box_BOXZEROBYTES]; | ||
184 | if (i == 0) m[crypto_box_ZEROBYTES] = 0; | ||
185 | m[i + crypto_box_ZEROBYTES] = m[crypto_box_ZEROBYTES]; | ||
186 | for (j = 0;j < i;++j) m[j + crypto_box_ZEROBYTES] ^= c[j + crypto_box_BOXZEROBYTES]; | ||
187 | } | ||
188 | |||
189 | for (i = 0;i < nlen;++i) { | ||
190 | checksum[2 * i] = "0123456789abcdef"[15 & (n[i] >> 4)]; | ||
191 | checksum[2 * i + 1] = "0123456789abcdef"[15 & n[i]]; | ||
192 | } | ||
193 | checksum[2 * i] = 0; | ||
194 | return 0; | ||
195 | } | ||
diff --git a/nacl/crypto_box/wrapper-box.cpp b/nacl/crypto_box/wrapper-box.cpp new file mode 100644 index 00000000..f0429295 --- /dev/null +++ b/nacl/crypto_box/wrapper-box.cpp | |||
@@ -0,0 +1,24 @@ | |||
1 | #include <string> | ||
2 | using std::string; | ||
3 | #include "crypto_box.h" | ||
4 | |||
5 | string crypto_box(const string &m,const string &n,const string &pk,const string &sk) | ||
6 | { | ||
7 | if (pk.size() != crypto_box_PUBLICKEYBYTES) throw "incorrect public-key length"; | ||
8 | if (sk.size() != crypto_box_SECRETKEYBYTES) throw "incorrect secret-key length"; | ||
9 | if (n.size() != crypto_box_NONCEBYTES) throw "incorrect nonce length"; | ||
10 | size_t mlen = m.size() + crypto_box_ZEROBYTES; | ||
11 | unsigned char mpad[mlen]; | ||
12 | for (int i = 0;i < crypto_box_ZEROBYTES;++i) mpad[i] = 0; | ||
13 | for (int i = crypto_box_ZEROBYTES;i < mlen;++i) mpad[i] = m[i - crypto_box_ZEROBYTES]; | ||
14 | unsigned char cpad[mlen]; | ||
15 | crypto_box(cpad,mpad,mlen, | ||
16 | (const unsigned char *) n.c_str(), | ||
17 | (const unsigned char *) pk.c_str(), | ||
18 | (const unsigned char *) sk.c_str() | ||
19 | ); | ||
20 | return string( | ||
21 | (char *) cpad + crypto_box_BOXZEROBYTES, | ||
22 | mlen - crypto_box_BOXZEROBYTES | ||
23 | ); | ||
24 | } | ||
diff --git a/nacl/crypto_box/wrapper-keypair.cpp b/nacl/crypto_box/wrapper-keypair.cpp new file mode 100644 index 00000000..b59f92d9 --- /dev/null +++ b/nacl/crypto_box/wrapper-keypair.cpp | |||
@@ -0,0 +1,12 @@ | |||
1 | #include <string> | ||
2 | using std::string; | ||
3 | #include "crypto_box.h" | ||
4 | |||
5 | string crypto_box_keypair(string *sk_string) | ||
6 | { | ||
7 | unsigned char pk[crypto_box_PUBLICKEYBYTES]; | ||
8 | unsigned char sk[crypto_box_SECRETKEYBYTES]; | ||
9 | crypto_box_keypair(pk,sk); | ||
10 | *sk_string = string((char *) sk,sizeof sk); | ||
11 | return string((char *) pk,sizeof pk); | ||
12 | } | ||
diff --git a/nacl/crypto_box/wrapper-open.cpp b/nacl/crypto_box/wrapper-open.cpp new file mode 100644 index 00000000..67663a21 --- /dev/null +++ b/nacl/crypto_box/wrapper-open.cpp | |||
@@ -0,0 +1,27 @@ | |||
1 | #include <string> | ||
2 | using std::string; | ||
3 | #include "crypto_box.h" | ||
4 | |||
5 | string crypto_box_open(const string &c,const string &n,const string &pk,const string &sk) | ||
6 | { | ||
7 | if (pk.size() != crypto_box_PUBLICKEYBYTES) throw "incorrect public-key length"; | ||
8 | if (sk.size() != crypto_box_SECRETKEYBYTES) throw "incorrect secret-key length"; | ||
9 | if (n.size() != crypto_box_NONCEBYTES) throw "incorrect nonce length"; | ||
10 | size_t clen = c.size() + crypto_box_BOXZEROBYTES; | ||
11 | unsigned char cpad[clen]; | ||
12 | for (int i = 0;i < crypto_box_BOXZEROBYTES;++i) cpad[i] = 0; | ||
13 | for (int i = crypto_box_BOXZEROBYTES;i < clen;++i) cpad[i] = c[i - crypto_box_BOXZEROBYTES]; | ||
14 | unsigned char mpad[clen]; | ||
15 | if (crypto_box_open(mpad,cpad,clen, | ||
16 | (const unsigned char *) n.c_str(), | ||
17 | (const unsigned char *) pk.c_str(), | ||
18 | (const unsigned char *) sk.c_str() | ||
19 | ) != 0) | ||
20 | throw "ciphertext fails verification"; | ||
21 | if (clen < crypto_box_ZEROBYTES) | ||
22 | throw "ciphertext too short"; // should have been caught by _open | ||
23 | return string( | ||
24 | (char *) mpad + crypto_box_ZEROBYTES, | ||
25 | clen - crypto_box_ZEROBYTES | ||
26 | ); | ||
27 | } | ||