Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libnacl / tests / secretbox6.cpp
blobe8274006d677f30fe3f62813ecd8b565b6acad6e
1 #include <string>
2 using std::string;
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "crypto_secretbox.h"
6 #include "randombytes.h"
8 main()
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;
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;
41 return 0;