1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-20 18:45:32 -07:00
libsodium/test/default/chacha20.c
Frank Denis c8e8983430 Test crypto_stream_*() with an output buffer previously filled with junk
Obviously, the previous content shouldn't change the output.
2015-07-21 13:37:06 +02:00

141 lines
5.4 KiB
C

#define TEST_NAME "chacha20"
#include "cmptest.h"
static
void tv(void)
{
static struct {
const char *key_hex;
const char *nonce_hex;
} tests[]
= { { "0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000" },
{ "0000000000000000000000000000000000000000000000000000000000000001",
"0000000000000000" },
{ "0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000001" },
{ "0000000000000000000000000000000000000000000000000000000000000000",
"0100000000000000" },
{ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"0001020304050607" } };
unsigned char key[crypto_stream_chacha20_KEYBYTES];
unsigned char nonce[crypto_stream_chacha20_NONCEBYTES];
unsigned char out[160];
char out_hex[160 * 2 + 1];
size_t i = 0U;
do {
sodium_hex2bin((unsigned char *)key, sizeof key, tests[i].key_hex,
strlen(tests[i].key_hex), NULL, NULL, NULL);
sodium_hex2bin(nonce, sizeof nonce, tests[i].nonce_hex,
strlen(tests[i].nonce_hex), NULL, NULL, NULL);
crypto_stream_chacha20(out, sizeof out, nonce, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
} while (++i < (sizeof tests) / (sizeof tests[0]));
randombytes_buf(out, sizeof out);
crypto_stream_chacha20(out, sizeof out, nonce, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
assert(crypto_stream_chacha20(out, 0U, nonce, key) == 0);
assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);
assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);
assert(crypto_stream_chacha20_xor_ic(out, out, 0U, nonce, 1U, key) == 0);
memset(out, 0x42, sizeof out);
crypto_stream_chacha20_xor(out, out, sizeof out, nonce, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 0U, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 1U, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
};
static
void tv_ietf(void)
{
static struct {
const char *key_hex;
const char *nonce_hex;
uint32_t ic;
} tests[]
= { { "0000000000000000000000000000000000000000000000000000000000000000",
"000000000000000000000000",
0U },
{ "0000000000000000000000000000000000000000000000000000000000000000",
"000000000000000000000000",
1U },
{ "0000000000000000000000000000000000000000000000000000000000000001",
"000000000000000000000000",
1U },
{ "00ff000000000000000000000000000000000000000000000000000000000000",
"000000000000000000000000",
2U },
{ "0000000000000000000000000000000000000000000000000000000000000000",
"000000000000000000000002",
0U },
{ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"000000090000004a00000000",
1U }};
unsigned char key[crypto_stream_chacha20_KEYBYTES];
unsigned char nonce[crypto_stream_chacha20_IETF_NONCEBYTES];
unsigned char out[160];
char out_hex[160 * 2 + 1];
size_t i = 0U;
do {
sodium_hex2bin((unsigned char *)key, sizeof key, tests[i].key_hex,
strlen(tests[i].key_hex), ": ", NULL, NULL);
sodium_hex2bin(nonce, sizeof nonce, tests[i].nonce_hex,
strlen(tests[i].nonce_hex), ": ", NULL, NULL);
memset(out, 0, sizeof out);
crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, tests[i].ic, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
} while (++i < (sizeof tests) / (sizeof tests[0]));
randombytes_buf(out, sizeof out);
crypto_stream_chacha20_ietf(out, sizeof out, nonce, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
assert(crypto_stream_chacha20_ietf(out, 0U, nonce, key) == 0);
assert(crypto_stream_chacha20_ietf_xor(out, out, 0U, nonce, key) == 0);
assert(crypto_stream_chacha20_ietf_xor(out, out, 0U, nonce, key) == 0);
assert(crypto_stream_chacha20_ietf_xor_ic(out, out, 0U, nonce, 1U, key) == 0);
memset(out, 0x42, sizeof out);
crypto_stream_chacha20_ietf_xor(out, out, sizeof out, nonce, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, 0U, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, 1U, key);
sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
printf("[%s]\n", out_hex);
};
int
main(void)
{
tv();
tv_ietf();
assert(crypto_stream_chacha20_keybytes() > 0U);
assert(crypto_stream_chacha20_noncebytes() > 0U);
assert(crypto_stream_chacha20_ietf_noncebytes() > 0U);
return 0;
}