xref: /freebsd/sys/contrib/libsodium/test/default/box_seal.c (revision 0ac341f145426f1f0d00b64d35f19fada2be635c)
1*0ac341f1SConrad Meyer 
2*0ac341f1SConrad Meyer #define TEST_NAME "box_seal"
3*0ac341f1SConrad Meyer #include "cmptest.h"
4*0ac341f1SConrad Meyer 
5*0ac341f1SConrad Meyer int
6*0ac341f1SConrad Meyer main(void)
7*0ac341f1SConrad Meyer {
8*0ac341f1SConrad Meyer     unsigned char  pk[crypto_box_PUBLICKEYBYTES];
9*0ac341f1SConrad Meyer     unsigned char  sk[crypto_box_SECRETKEYBYTES];
10*0ac341f1SConrad Meyer     unsigned char *c;
11*0ac341f1SConrad Meyer     unsigned char *m;
12*0ac341f1SConrad Meyer     unsigned char *m2;
13*0ac341f1SConrad Meyer     size_t         m_len;
14*0ac341f1SConrad Meyer     size_t         c_len;
15*0ac341f1SConrad Meyer 
16*0ac341f1SConrad Meyer     crypto_box_keypair(pk, sk);
17*0ac341f1SConrad Meyer     m_len = (size_t) randombytes_uniform(1000);
18*0ac341f1SConrad Meyer     c_len = crypto_box_SEALBYTES + m_len;
19*0ac341f1SConrad Meyer     m     = (unsigned char *) sodium_malloc(m_len);
20*0ac341f1SConrad Meyer     m2    = (unsigned char *) sodium_malloc(m_len);
21*0ac341f1SConrad Meyer     c     = (unsigned char *) sodium_malloc(c_len);
22*0ac341f1SConrad Meyer     randombytes_buf(m, m_len);
23*0ac341f1SConrad Meyer     if (crypto_box_seal(c, m, m_len, pk) != 0) {
24*0ac341f1SConrad Meyer         printf("crypto_box_seal() failure\n");
25*0ac341f1SConrad Meyer         return 1;
26*0ac341f1SConrad Meyer     }
27*0ac341f1SConrad Meyer     if (crypto_box_seal_open(m2, c, c_len, pk, sk) != 0) {
28*0ac341f1SConrad Meyer         printf("crypto_box_seal_open() failure\n");
29*0ac341f1SConrad Meyer         return 1;
30*0ac341f1SConrad Meyer     }
31*0ac341f1SConrad Meyer     printf("%d\n", memcmp(m, m2, m_len));
32*0ac341f1SConrad Meyer 
33*0ac341f1SConrad Meyer     printf("%d\n", crypto_box_seal_open(m, c, 0U, pk, sk));
34*0ac341f1SConrad Meyer     printf("%d\n", crypto_box_seal_open(m, c, c_len - 1U, pk, sk));
35*0ac341f1SConrad Meyer     printf("%d\n", crypto_box_seal_open(m, c, c_len, sk, pk));
36*0ac341f1SConrad Meyer 
37*0ac341f1SConrad Meyer     sodium_free(c);
38*0ac341f1SConrad Meyer     sodium_free(m);
39*0ac341f1SConrad Meyer     sodium_free(m2);
40*0ac341f1SConrad Meyer 
41*0ac341f1SConrad Meyer     assert(crypto_box_sealbytes() == crypto_box_SEALBYTES);
42*0ac341f1SConrad Meyer 
43*0ac341f1SConrad Meyer     return 0;
44*0ac341f1SConrad Meyer }
45