libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / test / default / secretbox8.c
blob94808dc9f616baf68350c0d711e23b38cc238b43
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "windows/windows-quirks.h"
5 #define TEST_NAME "secretbox8"
6 #include "cmptest.h"
8 unsigned char k[crypto_secretbox_KEYBYTES];
9 unsigned char n[crypto_secretbox_NONCEBYTES];
10 unsigned char m[10000];
11 unsigned char c[10000];
12 unsigned char m2[10000];
14 int main(void)
16 size_t mlen;
17 size_t i;
18 int caught;
20 for (mlen = 0;mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;++mlen) {
21 randombytes(k,crypto_secretbox_KEYBYTES);
22 randombytes(n,crypto_secretbox_NONCEBYTES);
23 randombytes(m + crypto_secretbox_ZEROBYTES,mlen);
24 crypto_secretbox(c,m,mlen + crypto_secretbox_ZEROBYTES,n,k);
25 caught = 0;
26 while (caught < 10) {
27 c[rand() % (mlen + crypto_secretbox_ZEROBYTES)] = rand();
28 if (crypto_secretbox_open(m2,c,mlen + crypto_secretbox_ZEROBYTES,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;
34 } else {
35 ++caught;
39 return 0;