diff options
Diffstat (limited to 'nacl/tests/secretbox7.c')
-rw-r--r-- | nacl/tests/secretbox7.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/nacl/tests/secretbox7.c b/nacl/tests/secretbox7.c new file mode 100644 index 00000000..d4be9b49 --- /dev/null +++ b/nacl/tests/secretbox7.c | |||
@@ -0,0 +1,32 @@ | |||
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 | |||
16 | for (mlen = 0;mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;++mlen) { | ||
17 | randombytes(k,crypto_secretbox_KEYBYTES); | ||
18 | randombytes(n,crypto_secretbox_NONCEBYTES); | ||
19 | randombytes(m + crypto_secretbox_ZEROBYTES,mlen); | ||
20 | crypto_secretbox(c,m,mlen + crypto_secretbox_ZEROBYTES,n,k); | ||
21 | if (crypto_secretbox_open(m2,c,mlen + crypto_secretbox_ZEROBYTES,n,k) == 0) { | ||
22 | for (i = 0;i < mlen + crypto_secretbox_ZEROBYTES;++i) | ||
23 | if (m2[i] != m[i]) { | ||
24 | printf("bad decryption\n"); | ||
25 | break; | ||
26 | } | ||
27 | } else { | ||
28 | printf("ciphertext fails verification\n"); | ||
29 | } | ||
30 | } | ||
31 | return 0; | ||
32 | } | ||