xref: /freebsd/sys/contrib/libsodium/test/default/box8.c (revision 0ac341f145426f1f0d00b64d35f19fada2be635c)
1*0ac341f1SConrad Meyer 
2*0ac341f1SConrad Meyer #define TEST_NAME "box8"
3*0ac341f1SConrad Meyer #include "cmptest.h"
4*0ac341f1SConrad Meyer 
5*0ac341f1SConrad Meyer static unsigned char alicesk[crypto_box_SECRETKEYBYTES];
6*0ac341f1SConrad Meyer static unsigned char alicepk[crypto_box_PUBLICKEYBYTES];
7*0ac341f1SConrad Meyer static unsigned char bobsk[crypto_box_SECRETKEYBYTES];
8*0ac341f1SConrad Meyer static unsigned char bobpk[crypto_box_PUBLICKEYBYTES];
9*0ac341f1SConrad Meyer static unsigned char n[crypto_box_NONCEBYTES];
10*0ac341f1SConrad Meyer 
11*0ac341f1SConrad Meyer int
12*0ac341f1SConrad Meyer main(void)
13*0ac341f1SConrad Meyer {
14*0ac341f1SConrad Meyer     unsigned char *m;
15*0ac341f1SConrad Meyer     unsigned char *c;
16*0ac341f1SConrad Meyer     unsigned char *m2;
17*0ac341f1SConrad Meyer     size_t         mlen;
18*0ac341f1SConrad Meyer     size_t         mlen_max = 1000;
19*0ac341f1SConrad Meyer     size_t         i;
20*0ac341f1SConrad Meyer     int            faults;
21*0ac341f1SConrad Meyer     int            ret;
22*0ac341f1SConrad Meyer 
23*0ac341f1SConrad Meyer     m  = (unsigned char *) sodium_malloc(mlen_max);
24*0ac341f1SConrad Meyer     c  = (unsigned char *) sodium_malloc(mlen_max);
25*0ac341f1SConrad Meyer     m2 = (unsigned char *) sodium_malloc(mlen_max);
26*0ac341f1SConrad Meyer     crypto_box_keypair(alicepk, alicesk);
27*0ac341f1SConrad Meyer     crypto_box_keypair(bobpk, bobsk);
28*0ac341f1SConrad Meyer     for (mlen = 0; mlen + crypto_box_ZEROBYTES <= mlen_max; mlen++) {
29*0ac341f1SConrad Meyer         randombytes_buf(n, crypto_box_NONCEBYTES);
30*0ac341f1SConrad Meyer         randombytes_buf(m + crypto_box_ZEROBYTES, mlen);
31*0ac341f1SConrad Meyer         ret = crypto_box(c, m, mlen + crypto_box_ZEROBYTES, n, bobpk, alicesk);
32*0ac341f1SConrad Meyer         assert(ret == 0);
33*0ac341f1SConrad Meyer #ifdef BROWSER_TESTS
34*0ac341f1SConrad Meyer         faults = 1;
35*0ac341f1SConrad Meyer #else
36*0ac341f1SConrad Meyer         faults = 5;
37*0ac341f1SConrad Meyer #endif
38*0ac341f1SConrad Meyer         while (faults > 0) {
39*0ac341f1SConrad Meyer             c[rand() % (mlen + crypto_box_ZEROBYTES)] = rand();
40*0ac341f1SConrad Meyer             if (crypto_box_open(m2, c, mlen + crypto_box_ZEROBYTES, n, alicepk,
41*0ac341f1SConrad Meyer                                 bobsk) == 0) {
42*0ac341f1SConrad Meyer                 for (i = 0; i < mlen + crypto_box_ZEROBYTES; ++i) {
43*0ac341f1SConrad Meyer                     if (m2[i] != m[i]) {
44*0ac341f1SConrad Meyer                         printf("forgery\n");
45*0ac341f1SConrad Meyer                         return 100;
46*0ac341f1SConrad Meyer                     }
47*0ac341f1SConrad Meyer                 }
48*0ac341f1SConrad Meyer             } else {
49*0ac341f1SConrad Meyer                 faults--;
50*0ac341f1SConrad Meyer             }
51*0ac341f1SConrad Meyer         }
52*0ac341f1SConrad Meyer     }
53*0ac341f1SConrad Meyer     sodium_free(m);
54*0ac341f1SConrad Meyer     sodium_free(c);
55*0ac341f1SConrad Meyer     sodium_free(m2);
56*0ac341f1SConrad Meyer 
57*0ac341f1SConrad Meyer     return 0;
58*0ac341f1SConrad Meyer }
59