2 #define TEST_NAME "chacha20"
10 const char *nonce_hex
;
12 = { { "0000000000000000000000000000000000000000000000000000000000000000",
14 { "0000000000000000000000000000000000000000000000000000000000000001",
16 { "0000000000000000000000000000000000000000000000000000000000000000",
18 { "0000000000000000000000000000000000000000000000000000000000000000",
20 { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
21 "0001020304050607" } };
22 unsigned char key
[crypto_stream_chacha20_KEYBYTES
];
23 unsigned char nonce
[crypto_stream_chacha20_NONCEBYTES
];
25 unsigned char out
[160];
26 unsigned char zero
[160];
27 char out_hex
[160 * 2 + 1];
31 memset(zero
, 0, sizeof zero
);
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
);
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
);
79 const char *nonce_hex
;
82 = { { "0000000000000000000000000000000000000000000000000000000000000000",
83 "000000000000000000000000",
85 { "0000000000000000000000000000000000000000000000000000000000000000",
86 "000000000000000000000000",
88 { "0000000000000000000000000000000000000000000000000000000000000001",
89 "000000000000000000000000",
91 { "00ff000000000000000000000000000000000000000000000000000000000000",
92 "000000000000000000000000",
94 { "0000000000000000000000000000000000000000000000000000000000000000",
95 "000000000000000000000002",
97 { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
98 "000000090000004a00000000",
100 unsigned char key
[crypto_stream_chacha20_KEYBYTES
];
101 unsigned char nonce
[crypto_stream_chacha20_IETF_NONCEBYTES
];
103 unsigned char out
[160];
104 unsigned char zero
[160];
105 char out_hex
[160 * 2 + 1];
109 memset(zero
, 0, sizeof zero
);
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
);
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
);
159 assert(crypto_stream_chacha20_keybytes() > 0U);
160 assert(crypto_stream_chacha20_noncebytes() > 0U);
161 assert(crypto_stream_chacha20_ietf_noncebytes() > 0U);