diff options
Diffstat (limited to 'nacl/tests/secretbox8.c')
-rw-r--r-- | nacl/tests/secretbox8.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/nacl/tests/secretbox8.c b/nacl/tests/secretbox8.c new file mode 100644 index 00000000..a6c75c23 --- /dev/null +++ b/nacl/tests/secretbox8.c | |||
@@ -0,0 +1,37 @@ | |||
1 | #include <stdio.h> | ||
2 | #include "crypto_secretbox.h" | ||
3 | #include "randombytes.h" | ||
4 | |||
5 | unsigned char k[crypto_secretbox_KEYBYTES]; | ||
6 | unsigned char n[crypto_secretbox_NONCEBYTES]; | ||
7 | unsigned char m[10000]; | ||
8 | unsigned char c[10000]; | ||
9 | unsigned char m2[10000]; | ||
10 | |||
11 | main() | ||
12 | { | ||
13 | int mlen; | ||
14 | int i; | ||
15 | int caught; | ||
16 | |||
17 | for (mlen = 0;mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;++mlen) { | ||
18 | randombytes(k,crypto_secretbox_KEYBYTES); | ||
19 | randombytes(n,crypto_secretbox_NONCEBYTES); | ||
20 | randombytes(m + crypto_secretbox_ZEROBYTES,mlen); | ||
21 | crypto_secretbox(c,m,mlen + crypto_secretbox_ZEROBYTES,n,k); | ||
22 | caught = 0; | ||
23 | while (caught < 10) { | ||
24 | c[random() % (mlen + crypto_secretbox_ZEROBYTES)] = random(); | ||
25 | if (crypto_secretbox_open(m2,c,mlen + crypto_secretbox_ZEROBYTES,n,k) == 0) { | ||
26 | for (i = 0;i < mlen + crypto_secretbox_ZEROBYTES;++i) | ||
27 | if (m2[i] != m[i]) { | ||
28 | printf("forgery\n"); | ||
29 | return 100; | ||
30 | } | ||
31 | } else { | ||
32 | ++caught; | ||
33 | } | ||
34 | } | ||
35 | } | ||
36 | return 0; | ||
37 | } | ||