xref: /freebsd/sys/contrib/libsodium/test/default/secretbox7.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 
2 #define TEST_NAME "secretbox7"
3 #include "cmptest.h"
4 
5 static unsigned char k[crypto_secretbox_KEYBYTES];
6 static unsigned char n[crypto_secretbox_NONCEBYTES];
7 static unsigned char m[10000];
8 static unsigned char c[10000];
9 static unsigned char m2[10000];
10 
11 int
12 main(void)
13 {
14     size_t mlen;
15     size_t i;
16 
17     for (mlen = 0; mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;
18          ++mlen) {
19         crypto_secretbox_keygen(k);
20         randombytes_buf(n, crypto_secretbox_NONCEBYTES);
21         randombytes_buf(m + crypto_secretbox_ZEROBYTES, mlen);
22         crypto_secretbox(c, m, mlen + crypto_secretbox_ZEROBYTES, n, k);
23         if (crypto_secretbox_open(m2, c, mlen + crypto_secretbox_ZEROBYTES, n,
24                                   k) == 0) {
25             for (i = 0; i < mlen + crypto_secretbox_ZEROBYTES; ++i) {
26                 if (m2[i] != m[i]) {
27                     printf("bad decryption\n");
28                     break;
29                 }
30             }
31         } else {
32             printf("ciphertext fails verification\n");
33         }
34     }
35     return 0;
36 }
37