libsodium/test/default/chacha20.c

60 lines
2.1 KiB
C
Raw Normal View History

2014-05-15 16:27:15 -04:00
#define TEST_NAME "chacha20"
#include "cmptest.h"
static void tv(void)
{
static struct {
const char *key_hex;
const char *nonce_hex;
2014-09-14 14:32:55 -04:00
} tests[]
= { { "0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000" },
{ "0000000000000000000000000000000000000000000000000000000000000001",
"0000000000000000" },
{ "0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000001" },
{ "0000000000000000000000000000000000000000000000000000000000000000",
"0100000000000000" },
{ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"0001020304050607" } };
2014-05-15 16:27:15 -04:00
unsigned char key[crypto_stream_chacha20_KEYBYTES];
unsigned char nonce[crypto_stream_chacha20_NONCEBYTES];
unsigned char out[60];
2014-09-14 14:32:55 -04:00
char out_hex[60 * 2 + 1];
size_t i = 0U;
2014-05-15 16:27:15 -04:00
do {
2014-09-14 14:32:55 -04:00
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);
2014-05-15 16:27:15 -04:00
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]));
2014-09-14 16:09:15 -04:00
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);
2014-05-15 16:27:15 -04:00
};
int main(void)
{
tv();
2014-09-13 18:12:23 -04:00
assert(crypto_stream_chacha20_keybytes() > 0U);
assert(crypto_stream_chacha20_noncebytes() > 0U);
return 0;
2014-05-15 16:27:15 -04:00
}