libsodium 1.0.8
[tomato.git] / release / src / router / libsodium / test / default / secretbox8.c
blobaceb978b532b565c1030ea74d8d832dad9af87d5
2 #define TEST_NAME "secretbox8"
3 #include "cmptest.h"
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];
11 int main(void)
13 size_t mlen;
14 size_t i;
15 int caught;
17 for (mlen = 0; mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;
18 ++mlen) {
19 randombytes_buf(k, crypto_secretbox_KEYBYTES);
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 caught = 0;
24 while (caught < 10) {
25 c[rand() % (mlen + crypto_secretbox_ZEROBYTES)] = rand();
26 if (crypto_secretbox_open(m2, c, mlen + crypto_secretbox_ZEROBYTES,
27 n, k) == 0) {
28 for (i = 0; i < mlen + crypto_secretbox_ZEROBYTES; ++i) {
29 if (m2[i] != m[i]) {
30 printf("forgery\n");
31 return 100;
34 } else {
35 ++caught;
39 return 0;