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