Path: blob/main/sys/contrib/libsodium/test/default/chacha20.c
48375 views
1#define TEST_NAME "chacha20"2#include "cmptest.h"34static5void tv(void)6{7static struct {8const char *key_hex;9const char *nonce_hex;10} tests[]11= { { "0000000000000000000000000000000000000000000000000000000000000000",12"0000000000000000" },13{ "0000000000000000000000000000000000000000000000000000000000000001",14"0000000000000000" },15{ "0000000000000000000000000000000000000000000000000000000000000000",16"0000000000000001" },17{ "0000000000000000000000000000000000000000000000000000000000000000",18"0100000000000000" },19{ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",20"0001020304050607" } };21unsigned char key[crypto_stream_chacha20_KEYBYTES];22unsigned char nonce[crypto_stream_chacha20_NONCEBYTES];23unsigned char *part;24unsigned char out[160];25unsigned char zero[160];26char out_hex[160 * 2 + 1];27size_t i = 0U;28size_t plen;2930memset(zero, 0, sizeof zero);31do {32sodium_hex2bin((unsigned char *)key, sizeof key, tests[i].key_hex,33strlen(tests[i].key_hex), NULL, NULL, NULL);34sodium_hex2bin(nonce, sizeof nonce, tests[i].nonce_hex,35strlen(tests[i].nonce_hex), NULL, NULL, NULL);36crypto_stream_chacha20(out, sizeof out, nonce, key);37sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);38printf("[%s]\n", out_hex);39for (plen = 1U; plen < sizeof out; plen++) {40part = (unsigned char *) sodium_malloc(plen);41crypto_stream_chacha20_xor(part, out, plen, nonce, key);42if (memcmp(part, zero, plen) != 0) {43printf("Failed with length %lu\n", (unsigned long) plen);44}45sodium_free(part);46}47} while (++i < (sizeof tests) / (sizeof tests[0]));48assert(66 <= sizeof out);49for (plen = 1U; plen < 66; plen += 3) {50memset(out, (int) (plen & 0xff), sizeof out);51crypto_stream_chacha20(out, plen, nonce, key);52sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);53printf("[%s]\n", out_hex);54}55randombytes_buf(out, sizeof out);56crypto_stream_chacha20(out, sizeof out, nonce, key);57sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);58printf("[%s]\n", out_hex);5960assert(crypto_stream_chacha20(out, 0U, nonce, key) == 0);61assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);62assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);63assert(crypto_stream_chacha20_xor_ic(out, out, 0U, nonce, 1U, key) == 0);6465memset(out, 0x42, sizeof out);66crypto_stream_chacha20_xor(out, out, sizeof out, nonce, key);67sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);68printf("[%s]\n", out_hex);6970crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 0U, key);71sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);72printf("[%s]\n", out_hex);7374crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 1U, key);75sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);76printf("[%s]\n", out_hex);77}7879static80void tv_ietf(void)81{82static struct {83const char *key_hex;84const char *nonce_hex;85uint32_t ic;86} tests[]87= { { "0000000000000000000000000000000000000000000000000000000000000000",88"000000000000000000000000",890U },90{ "0000000000000000000000000000000000000000000000000000000000000000",91"000000000000000000000000",921U },93{ "0000000000000000000000000000000000000000000000000000000000000001",94"000000000000000000000000",951U },96{ "00ff000000000000000000000000000000000000000000000000000000000000",97"000000000000000000000000",982U },99{ "0000000000000000000000000000000000000000000000000000000000000000",100"000000000000000000000002",1010U },102{ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",103"000000090000004a00000000",1041U },105{ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",106"000000090000004a00000000",1070xffffffff }};108unsigned char key[crypto_stream_chacha20_KEYBYTES];109unsigned char nonce[crypto_stream_chacha20_IETF_NONCEBYTES];110unsigned char *part;111unsigned char out[160];112unsigned char zero[160];113char out_hex[160 * 2 + 1];114size_t i = 0U;115size_t plen;116117memset(zero, 0, sizeof zero);118do {119sodium_hex2bin((unsigned char *)key, sizeof key, tests[i].key_hex,120strlen(tests[i].key_hex), ": ", NULL, NULL);121sodium_hex2bin(nonce, sizeof nonce, tests[i].nonce_hex,122strlen(tests[i].nonce_hex), ": ", NULL, NULL);123memset(out, 0, sizeof out);124crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, tests[i].ic, key);125sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);126printf("[%s]\n", out_hex);127for (plen = 1U; plen < sizeof out; plen++) {128part = (unsigned char *) sodium_malloc(plen);129crypto_stream_chacha20_ietf_xor_ic(part, out, plen, nonce, tests[i].ic, key);130if (memcmp(part, zero, plen) != 0) {131printf("Failed with length %lu\n", (unsigned long) plen);132}133sodium_free(part);134}135} while (++i < (sizeof tests) / (sizeof tests[0]));136assert(66 <= sizeof out);137for (plen = 1U; plen < 66; plen += 3) {138memset(out, (int) (plen & 0xff), sizeof out);139crypto_stream_chacha20(out, plen, nonce, key);140sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);141printf("[%s]\n", out_hex);142}143randombytes_buf(out, sizeof out);144crypto_stream_chacha20_ietf(out, sizeof out, nonce, key);145sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);146printf("[%s]\n", out_hex);147148assert(crypto_stream_chacha20_ietf(out, 0U, nonce, key) == 0);149assert(crypto_stream_chacha20_ietf_xor(out, out, 0U, nonce, key) == 0);150assert(crypto_stream_chacha20_ietf_xor(out, out, 0U, nonce, key) == 0);151assert(crypto_stream_chacha20_ietf_xor_ic(out, out, 0U, nonce, 1U, key) == 0);152153memset(out, 0x42, sizeof out);154crypto_stream_chacha20_ietf_xor(out, out, sizeof out, nonce, key);155sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);156printf("[%s]\n", out_hex);157158crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, 0U, key);159sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);160printf("[%s]\n", out_hex);161162crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, 1U, key);163sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);164printf("[%s]\n", out_hex);165}166167int168main(void)169{170tv();171tv_ietf();172173assert(crypto_stream_chacha20_keybytes() > 0U);174assert(crypto_stream_chacha20_keybytes() == crypto_stream_chacha20_KEYBYTES);175assert(crypto_stream_chacha20_noncebytes() > 0U);176assert(crypto_stream_chacha20_noncebytes() == crypto_stream_chacha20_NONCEBYTES);177assert(crypto_stream_chacha20_messagebytes_max() == crypto_stream_chacha20_MESSAGEBYTES_MAX);178assert(crypto_stream_chacha20_ietf_keybytes() > 0U);179assert(crypto_stream_chacha20_ietf_keybytes() == crypto_stream_chacha20_ietf_KEYBYTES);180assert(crypto_stream_chacha20_ietf_noncebytes() > 0U);181assert(crypto_stream_chacha20_ietf_noncebytes() == crypto_stream_chacha20_ietf_NONCEBYTES);182assert(crypto_stream_chacha20_ietf_messagebytes_max() == crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX);183184return 0;185}186187188