libsodium 1.0.8
[tomato.git] / release / src / router / libsodium / test / default / box7.c
blob3ef6a6161a1a6d654c6a483822f7cd6f6fe09dfe
2 #define TEST_NAME "box7"
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 ret;
21 m = (unsigned char *) sodium_malloc(mlen_max);
22 c = (unsigned char *) sodium_malloc(mlen_max);
23 m2 = (unsigned char *) sodium_malloc(mlen_max);
24 memset(m, 0, crypto_box_ZEROBYTES);
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 if (crypto_box_open(m2, c, mlen + crypto_box_ZEROBYTES, n, alicepk,
33 bobsk) == 0) {
34 for (i = 0; i < mlen + crypto_box_ZEROBYTES; ++i) {
35 if (m2[i] != m[i]) {
36 printf("bad decryption\n");
37 break;
40 } else {
41 printf("ciphertext fails verification\n");
44 sodium_free(m);
45 sodium_free(c);
46 sodium_free(m2);
48 return 0;