xref: /freebsd/sys/contrib/libsodium/test/default/secretbox8.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1 
2 #define TEST_NAME "secretbox8"
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     int    caught;
17 
18     for (mlen = 0; mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;
19          ++mlen) {
20         crypto_secretbox_keygen(k);
21         randombytes_buf(n, crypto_secretbox_NONCEBYTES);
22         randombytes_buf(m + crypto_secretbox_ZEROBYTES, mlen);
23         crypto_secretbox(c, m, mlen + crypto_secretbox_ZEROBYTES, n, k);
24         caught = 0;
25         while (caught < 10) {
26             c[rand() % (mlen + crypto_secretbox_ZEROBYTES)] = rand();
27             if (crypto_secretbox_open(m2, c, mlen + crypto_secretbox_ZEROBYTES,
28                                       n, k) == 0) {
29                 for (i = 0; i < mlen + crypto_secretbox_ZEROBYTES; ++i) {
30                     if (m2[i] != m[i]) {
31                         printf("forgery\n");
32                         return 100;
33                     }
34                 }
35             } else {
36                 ++caught;
37             }
38         }
39     }
40     return 0;
41 }
42