libsodium 1.0.8
[tomato.git] / release / src / router / libsodium / test / default / box8.c
blob776294cda5add6047831a883fe853329c25ae43a
2 #define TEST_NAME "box8"
3 #include "cmptest.h"
5 static unsigned char alicesk[crypto_box_SECRETKEYBYTES];
6 static unsigned char alicepk[crypto_box_PUBLICKEYBYTES];
7 static unsigned char bobsk[crypto_box_SECRETKEYBYTES];
8 static unsigned char bobpk[crypto_box_PUBLICKEYBYTES];
9 static unsigned char n[crypto_box_NONCEBYTES];
11 int main(void)
13 unsigned char *m;
14 unsigned char *c;
15 unsigned char *m2;
16 size_t mlen;
17 size_t mlen_max = 1000;
18 size_t i;
19 int faults;
20 int ret;
22 m = (unsigned char *) sodium_malloc(mlen_max);
23 c = (unsigned char *) sodium_malloc(mlen_max);
24 m2 = (unsigned char *) sodium_malloc(mlen_max);
25 crypto_box_keypair(alicepk, alicesk);
26 crypto_box_keypair(bobpk, bobsk);
27 for (mlen = 0; mlen + crypto_box_ZEROBYTES <= mlen_max; mlen++) {
28 randombytes_buf(n, crypto_box_NONCEBYTES);
29 randombytes_buf(m + crypto_box_ZEROBYTES, mlen);
30 ret = crypto_box(c, m, mlen + crypto_box_ZEROBYTES, n, bobpk, alicesk);
31 assert(ret == 0);
32 #ifdef BROWSER_TESTS
33 faults = 1;
34 #else
35 faults = 5;
36 #endif
37 while (faults > 0) {
38 c[rand() % (mlen + crypto_box_ZEROBYTES)] = rand();
39 if (crypto_box_open(m2, c, mlen + crypto_box_ZEROBYTES, n, alicepk,
40 bobsk) == 0) {
41 for (i = 0; i < mlen + crypto_box_ZEROBYTES; ++i) {
42 if (m2[i] != m[i]) {
43 printf("forgery\n");
44 return 100;
47 } else {
48 faults--;
52 sodium_free(m);
53 sodium_free(c);
54 sodium_free(m2);
56 return 0;