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