libsodium 1.0.8
[tomato.git] / release / src / router / libsodium / test / default / chacha20.c
blob010efb404771c4baf02f1e22e7a3db778a838f5e
2 #define TEST_NAME "chacha20"
3 #include "cmptest.h"
5 static
6 void tv(void)
8 static struct {
9 const char *key_hex;
10 const char *nonce_hex;
11 } tests[]
12 = { { "0000000000000000000000000000000000000000000000000000000000000000",
13 "0000000000000000" },
14 { "0000000000000000000000000000000000000000000000000000000000000001",
15 "0000000000000000" },
16 { "0000000000000000000000000000000000000000000000000000000000000000",
17 "0000000000000001" },
18 { "0000000000000000000000000000000000000000000000000000000000000000",
19 "0100000000000000" },
20 { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
21 "0001020304050607" } };
22 unsigned char key[crypto_stream_chacha20_KEYBYTES];
23 unsigned char nonce[crypto_stream_chacha20_NONCEBYTES];
24 unsigned char *part;
25 unsigned char out[160];
26 unsigned char zero[160];
27 char out_hex[160 * 2 + 1];
28 size_t i = 0U;
29 size_t plen;
31 memset(zero, 0, sizeof zero);
32 do {
33 sodium_hex2bin((unsigned char *)key, sizeof key, tests[i].key_hex,
34 strlen(tests[i].key_hex), NULL, NULL, NULL);
35 sodium_hex2bin(nonce, sizeof nonce, tests[i].nonce_hex,
36 strlen(tests[i].nonce_hex), NULL, NULL, NULL);
37 crypto_stream_chacha20(out, sizeof out, nonce, key);
38 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
39 printf("[%s]\n", out_hex);
40 for (plen = 1U; plen < sizeof out; plen++) {
41 part = (unsigned char *) sodium_malloc(plen);
42 crypto_stream_chacha20_xor(part, out, plen, nonce, key);
43 if (memcmp(part, zero, plen) != 0) {
44 printf("Failed with length %lu\n", (unsigned long) plen);
46 sodium_free(part);
48 } while (++i < (sizeof tests) / (sizeof tests[0]));
50 randombytes_buf(out, sizeof out);
51 crypto_stream_chacha20(out, sizeof out, nonce, key);
52 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
53 printf("[%s]\n", out_hex);
55 assert(crypto_stream_chacha20(out, 0U, nonce, key) == 0);
56 assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);
57 assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);
58 assert(crypto_stream_chacha20_xor_ic(out, out, 0U, nonce, 1U, key) == 0);
60 memset(out, 0x42, sizeof out);
61 crypto_stream_chacha20_xor(out, out, sizeof out, nonce, key);
62 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
63 printf("[%s]\n", out_hex);
65 crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 0U, key);
66 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
67 printf("[%s]\n", out_hex);
69 crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 1U, key);
70 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
71 printf("[%s]\n", out_hex);
74 static
75 void tv_ietf(void)
77 static struct {
78 const char *key_hex;
79 const char *nonce_hex;
80 uint32_t ic;
81 } tests[]
82 = { { "0000000000000000000000000000000000000000000000000000000000000000",
83 "000000000000000000000000",
84 0U },
85 { "0000000000000000000000000000000000000000000000000000000000000000",
86 "000000000000000000000000",
87 1U },
88 { "0000000000000000000000000000000000000000000000000000000000000001",
89 "000000000000000000000000",
90 1U },
91 { "00ff000000000000000000000000000000000000000000000000000000000000",
92 "000000000000000000000000",
93 2U },
94 { "0000000000000000000000000000000000000000000000000000000000000000",
95 "000000000000000000000002",
96 0U },
97 { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
98 "000000090000004a00000000",
99 1U }};
100 unsigned char key[crypto_stream_chacha20_KEYBYTES];
101 unsigned char nonce[crypto_stream_chacha20_IETF_NONCEBYTES];
102 unsigned char *part;
103 unsigned char out[160];
104 unsigned char zero[160];
105 char out_hex[160 * 2 + 1];
106 size_t i = 0U;
107 size_t plen;
109 memset(zero, 0, sizeof zero);
110 do {
111 sodium_hex2bin((unsigned char *)key, sizeof key, tests[i].key_hex,
112 strlen(tests[i].key_hex), ": ", NULL, NULL);
113 sodium_hex2bin(nonce, sizeof nonce, tests[i].nonce_hex,
114 strlen(tests[i].nonce_hex), ": ", NULL, NULL);
115 memset(out, 0, sizeof out);
116 crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, tests[i].ic, key);
117 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
118 printf("[%s]\n", out_hex);
119 for (plen = 1U; plen < sizeof out; plen++) {
120 part = (unsigned char *) sodium_malloc(plen);
121 crypto_stream_chacha20_ietf_xor_ic(part, out, plen, nonce, tests[i].ic, key);
122 if (memcmp(part, zero, plen) != 0) {
123 printf("Failed with length %lu\n", (unsigned long) plen);
125 sodium_free(part);
127 } while (++i < (sizeof tests) / (sizeof tests[0]));
129 randombytes_buf(out, sizeof out);
130 crypto_stream_chacha20_ietf(out, sizeof out, nonce, key);
131 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
132 printf("[%s]\n", out_hex);
134 assert(crypto_stream_chacha20_ietf(out, 0U, nonce, key) == 0);
135 assert(crypto_stream_chacha20_ietf_xor(out, out, 0U, nonce, key) == 0);
136 assert(crypto_stream_chacha20_ietf_xor(out, out, 0U, nonce, key) == 0);
137 assert(crypto_stream_chacha20_ietf_xor_ic(out, out, 0U, nonce, 1U, key) == 0);
139 memset(out, 0x42, sizeof out);
140 crypto_stream_chacha20_ietf_xor(out, out, sizeof out, nonce, key);
141 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
142 printf("[%s]\n", out_hex);
144 crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, 0U, key);
145 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
146 printf("[%s]\n", out_hex);
148 crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, 1U, key);
149 sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
150 printf("[%s]\n", out_hex);
154 main(void)
156 tv();
157 tv_ietf();
159 assert(crypto_stream_chacha20_keybytes() > 0U);
160 assert(crypto_stream_chacha20_noncebytes() > 0U);
161 assert(crypto_stream_chacha20_ietf_noncebytes() > 0U);
163 return 0;