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