xref: /freebsd/sys/contrib/libsodium/test/default/box7.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1 
2 #define TEST_NAME "box7"
3 #include "cmptest.h"
4 
5 static unsigned char alicesk[crypto_box_SECRETKEYBYTES];
6 static unsigned char alicepk[crypto_box_PUBLICKEYBYTES];
7 static unsigned char bobsk[crypto_box_SECRETKEYBYTES];
8 static unsigned char bobpk[crypto_box_PUBLICKEYBYTES];
9 static unsigned char n[crypto_box_NONCEBYTES];
10 
11 int
12 main(void)
13 {
14     unsigned char *m;
15     unsigned char *c;
16     unsigned char *m2;
17     size_t         mlen;
18     size_t         mlen_max = 1000;
19     size_t         i;
20     int            ret;
21 
22     m  = (unsigned char *) sodium_malloc(mlen_max);
23     c  = (unsigned char *) sodium_malloc(mlen_max);
24     m2 = (unsigned char *) sodium_malloc(mlen_max);
25     memset(m, 0, crypto_box_ZEROBYTES);
26     crypto_box_keypair(alicepk, alicesk);
27     crypto_box_keypair(bobpk, bobsk);
28     for (mlen = 0; mlen + crypto_box_ZEROBYTES <= mlen_max; mlen++) {
29         randombytes_buf(n, crypto_box_NONCEBYTES);
30         randombytes_buf(m + crypto_box_ZEROBYTES, mlen);
31         ret = crypto_box(c, m, mlen + crypto_box_ZEROBYTES, n, bobpk, alicesk);
32         assert(ret == 0);
33         if (crypto_box_open(m2, c, mlen + crypto_box_ZEROBYTES, n, alicepk,
34                             bobsk) == 0) {
35             for (i = 0; i < mlen + crypto_box_ZEROBYTES; ++i) {
36                 if (m2[i] != m[i]) {
37                     printf("bad decryption\n");
38                     break;
39                 }
40             }
41         } else {
42             printf("ciphertext fails verification\n");
43         }
44     }
45     sodium_free(m);
46     sodium_free(c);
47     sodium_free(m2);
48 
49     return 0;
50 }
51