1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "crypto_box.h"
4 #include "randombytes.h"
5 
6 unsigned char alicesk[crypto_box_SECRETKEYBYTES];
7 unsigned char alicepk[crypto_box_PUBLICKEYBYTES];
8 unsigned char bobsk[crypto_box_SECRETKEYBYTES];
9 unsigned char bobpk[crypto_box_PUBLICKEYBYTES];
10 unsigned char n[crypto_box_NONCEBYTES];
11 unsigned char m[10000];
12 unsigned char c[10000];
13 unsigned char m2[10000];
14 
main()15 int main()
16 {
17   int mlen;
18   int i;
19   int caught;
20 
21   for (mlen = 0;mlen < 1000 && mlen + crypto_box_ZEROBYTES < sizeof m;++mlen) {
22     crypto_box_keypair(alicepk,alicesk);
23     crypto_box_keypair(bobpk,bobsk);
24     randombytes(n,crypto_box_NONCEBYTES);
25     randombytes(m + crypto_box_ZEROBYTES,mlen);
26     crypto_box(c,m,mlen + crypto_box_ZEROBYTES,n,bobpk,alicesk);
27     caught = 0;
28     while (caught < 10) {
29       c[random() % (mlen + crypto_box_ZEROBYTES)] = random();
30       if (crypto_box_open(m2,c,mlen + crypto_box_ZEROBYTES,n,alicepk,bobsk) == 0) {
31         for (i = 0;i < mlen + crypto_box_ZEROBYTES;++i)
32           if (m2[i] != m[i]) {
33 	    printf("forgery\n");
34 	    return 100;
35 	  }
36       } else {
37         ++caught;
38       }
39     }
40   }
41   return 0;
42 }
43