summaryrefslogtreecommitdiff
path: root/nacl/tests/box6.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/tests/box6.cpp')
-rw-r--r--nacl/tests/box6.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/nacl/tests/box6.cpp b/nacl/tests/box6.cpp
new file mode 100644
index 00000000..bab18105
--- /dev/null
+++ b/nacl/tests/box6.cpp
@@ -0,0 +1,43 @@
1#include <string>
2using std::string;
3#include <stdlib.h>
4#include <stdio.h>
5#include "crypto_box.h"
6#include "randombytes.h"
7
8main()
9{
10 int mlen;
11 for (mlen = 0;mlen < 1000;++mlen) {
12 string alicesk;
13 string alicepk = crypto_box_keypair(&alicesk);
14 string bobsk;
15 string bobpk = crypto_box_keypair(&bobsk);
16 unsigned char nbytes[crypto_box_NONCEBYTES];
17 randombytes(nbytes,crypto_box_NONCEBYTES);
18 string n((char *) nbytes,crypto_box_NONCEBYTES);
19 unsigned char mbytes[mlen];
20 randombytes(mbytes,mlen);
21 string m((char *) mbytes,mlen);
22 string c = crypto_box(m,n,bobpk,alicesk);
23 int caught = 0;
24 while (caught < 10) {
25 c.replace(random() % c.size(),1,1,random());
26 try {
27 string m2 = crypto_box_open(c,n,alicepk,bobsk);
28 if (m != m2) {
29 printf("forgery\n");
30 return 100;
31 }
32 } catch(const char *s) {
33 if (string(s) == string("ciphertext fails verification"))
34 ++caught;
35 else {
36 printf("%s\n",s);
37 return 111;
38 }
39 }
40 }
41 }
42 return 0;
43}