libsodium 1.0.8
[tomato.git] / release / src / router / libsodium / test / default / box_easy2.c
blob710f997c9fac7fc2b1bef1dd8b157a12b2d8c370
2 #define TEST_NAME "box_easy2"
3 #include "cmptest.h"
5 static const unsigned char small_order_p[crypto_box_PUBLICKEYBYTES]
6 = { 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3,
7 0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32,
8 0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00 };
10 int main(void)
12 unsigned char *alicepk;
13 unsigned char *alicesk;
14 unsigned char *bobpk;
15 unsigned char *bobsk;
16 unsigned char *mac;
17 unsigned char *nonce;
18 unsigned char *k1;
19 unsigned char *k2;
20 unsigned char *m;
21 unsigned char *m2;
22 unsigned char *c;
23 size_t mlen;
24 size_t i;
25 size_t m_size;
26 size_t m2_size;
27 size_t c_size;
28 int ret;
30 m2_size = m_size = 1U + randombytes_uniform(1000);
31 c_size = crypto_box_MACBYTES + m_size;
32 m = (unsigned char *) sodium_malloc(m_size);
33 m2 = (unsigned char *) sodium_malloc(m2_size);
34 c = (unsigned char *) sodium_malloc(c_size);
35 alicepk = (unsigned char *) sodium_malloc(crypto_box_PUBLICKEYBYTES);
36 alicesk = (unsigned char *) sodium_malloc(crypto_box_SECRETKEYBYTES);
37 bobpk = (unsigned char *) sodium_malloc(crypto_box_PUBLICKEYBYTES);
38 bobsk = (unsigned char *) sodium_malloc(crypto_box_SECRETKEYBYTES);
39 mac = (unsigned char *) sodium_malloc(crypto_box_MACBYTES);
40 nonce = (unsigned char *) sodium_malloc(crypto_box_NONCEBYTES);
41 k1 = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
42 k2 = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
43 crypto_box_keypair(alicepk, alicesk);
44 crypto_box_keypair(bobpk, bobsk);
45 mlen = (size_t) randombytes_uniform((uint32_t) m_size) + 1U;
46 randombytes_buf(m, mlen);
47 randombytes_buf(nonce, crypto_box_NONCEBYTES);
48 ret = crypto_box_easy(c, m, mlen, nonce, bobpk, alicesk);
49 assert(ret == 0);
50 if (crypto_box_open_easy(m2, c,
51 (unsigned long long) mlen + crypto_box_MACBYTES,
52 nonce, alicepk, bobsk) != 0) {
53 printf("open() failed");
54 return 1;
56 printf("%d\n", memcmp(m, m2, mlen));
58 for (i = 0; i < mlen + crypto_box_MACBYTES - 1; i++) {
59 if (crypto_box_open_easy(m2, c, (unsigned long long) i,
60 nonce, alicepk, bobsk) == 0) {
61 printf("short open() should have failed");
62 return 1;
65 memcpy(c, m, mlen);
66 ret = crypto_box_easy(c, c, (unsigned long long) mlen, nonce, bobpk, alicesk);
67 assert(ret == 0);
68 printf("%d\n", memcmp(m, c, mlen) == 0);
69 printf("%d\n", memcmp(m, c + crypto_box_MACBYTES, mlen) == 0);
70 if (crypto_box_open_easy(c, c,
71 (unsigned long long) mlen + crypto_box_MACBYTES,
72 nonce, alicepk, bobsk) != 0) {
73 printf("crypto_box_open_easy() failed\n");
76 ret = crypto_box_beforenm(k1, small_order_p, bobsk);
77 assert(ret == -1);
78 ret = crypto_box_beforenm(k2, small_order_p, alicesk);
79 assert(ret == -1);
81 ret = crypto_box_beforenm(k1, alicepk, bobsk);
82 assert(ret == 0);
83 ret = crypto_box_beforenm(k2, bobpk, alicesk);
84 assert(ret == 0);
86 memset(m2, 0, m2_size);
88 if (crypto_box_easy_afternm(c, m, SIZE_MAX - 1U, nonce, k1) == 0) {
89 printf("crypto_box_easy_afternm() with a short ciphertext should have failed\n");
91 crypto_box_easy_afternm(c, m, (unsigned long long) mlen, nonce, k1);
92 if (crypto_box_open_easy_afternm(m2, c,
93 (unsigned long long) mlen + crypto_box_MACBYTES,
94 nonce, k2) != 0) {
95 printf("crypto_box_open_easy_afternm() failed\n");
97 printf("%d\n", memcmp(m, m2, mlen));
98 if (crypto_box_open_easy_afternm(m2, c, crypto_box_MACBYTES - 1U,
99 nonce, k2) == 0) {
100 printf("crypto_box_open_easy_afternm() with a huge ciphertext should have failed\n");
102 memset(m2, 0, m2_size);
103 ret = crypto_box_detached(c, mac, m, (unsigned long long) mlen,
104 nonce, small_order_p, bobsk);
105 assert(ret == -1);
106 ret = crypto_box_detached(c, mac, m, (unsigned long long) mlen,
107 nonce, alicepk, bobsk);
108 assert(ret == 0);
109 if (crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen,
110 nonce, bobpk, alicesk) != 0) {
111 printf("crypto_box_open_detached() failed\n");
113 printf("%d\n", memcmp(m, m2, mlen));
115 memset(m2, 0, m2_size);
116 crypto_box_detached_afternm(c, mac, m, (unsigned long long) mlen,
117 nonce, k1);
118 if (crypto_box_open_detached_afternm(m2, c, mac, (unsigned long long) mlen,
119 nonce, k2) != 0) {
120 printf("crypto_box_open_detached_afternm() failed\n");
122 printf("%d\n", memcmp(m, m2, mlen));
124 sodium_free(alicepk);
125 sodium_free(alicesk);
126 sodium_free(bobpk);
127 sodium_free(bobsk);
128 sodium_free(mac);
129 sodium_free(nonce);
130 sodium_free(k1);
131 sodium_free(k2);
132 sodium_free(m);
133 sodium_free(m2);
134 sodium_free(c);
135 printf("OK\n");
137 return 0;