Skip to content

Commit

Permalink
tests: improve ChaCha20 unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Jul 7, 2023
1 parent 511a8d4 commit 7f2a985
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/test/crypto_tests.cpp
Expand Up @@ -480,6 +480,25 @@ BOOST_AUTO_TEST_CASE(aes_cbc_testvectors) {

BOOST_AUTO_TEST_CASE(chacha20_testvector)
{
/* Example from RFC8439 section 2.3.2. */
TestChaCha20("",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
{0x09000000, 0x4a000000}, 1,
"10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4e"
"d2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e");

/* Example from RFC8439 section 2.4.2. */
TestChaCha20("4c616469657320616e642047656e746c656d656e206f662074686520636c6173"
"73206f66202739393a204966204920636f756c64206f6666657220796f75206f"
"6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73"
"637265656e20776f756c642062652069742e",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
{0, 0x4a000000}, 1,
"6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0b"
"f91b65c5524733ab8f593dabcd62b3571639d624e65152ab8f530c359f0861d8"
"07ca0dbf500d6a6156a38e088a22b65e52bc514d16ccf806818ce91ab7793736"
"5af90bbf74a35be6b40b8eedf2785e42874d");

// RFC 7539/8439 A.1 Test Vector #1:
TestChaCha20("",
"0000000000000000000000000000000000000000000000000000000000000000",
Expand Down Expand Up @@ -563,6 +582,24 @@ BOOST_AUTO_TEST_CASE(chacha20_testvector)
"f39c6402c42234e32a356b3e764312a61a5532055716ead6962568f87d3f3f77"
"04c6a8d1bcd1bf4d50d6154b6da731b187b58dfd728afa36757a797ac188d1");

// RFC 7539/8439 A.4 Test Vector #1:
TestChaCha20("",
"0000000000000000000000000000000000000000000000000000000000000000",
{0, 0}, 0,
"76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7");

// RFC 7539/8439 A.4 Test Vector #2:
TestChaCha20("",
"0000000000000000000000000000000000000000000000000000000000000001",
{0, 0x200000000000000}, 0,
"ecfa254f845f647473d3cb140da9e87606cb33066c447b87bc2666dde3fbb739");

// RFC 7539/8439 A.4 Test Vector #3:
TestChaCha20("",
"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0",
{0, 0x200000000000000}, 0,
"965e3bc6f9ec7ed9560808f4d229f94b137ff275ca9b3fcbdd59deaad23310ae");

// test encryption
TestChaCha20("4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756"
"c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e"
Expand All @@ -578,6 +615,41 @@ BOOST_AUTO_TEST_CASE(chacha20_testvector)
"224f51f3401bd9e12fde276fb8631ded8c131f823d2c06e27e4fcaec9ef3cf788a3b0aa372600a92b57974cded2b9334794cb"
"a40c63e34cdea212c4cf07d41b769a6749f3f630f4122cafe28ec4dc47e26d4346d70b98c73f3e9c53ac40c5945398b6eda1a"
"832c89c167eacd901d7e2bf363");

// Test vectors from https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
// The first one is identical to the above one from the RFC8439 A.1 vectors, but repeated here
// for completeness.
TestChaCha20("",
"0000000000000000000000000000000000000000000000000000000000000000",
{0, 0}, 0,
"76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7"
"da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586");
TestChaCha20("",
"0000000000000000000000000000000000000000000000000000000000000001",
{0, 0}, 0,
"4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41"
"bbe2a0b6ea7566d2a5d1e7e20d42af2c53d792b1c43fea817e9ad275ae546963");
TestChaCha20("",
"0000000000000000000000000000000000000000000000000000000000000000",
{0, 0x0100000000000000ULL}, 0,
"de9cba7bf3d69ef5e786dc63973f653a0b49e015adbff7134fcb7df137821031"
"e85a050278a7084527214f73efc7fa5b5277062eb7a0433e445f41e3");
TestChaCha20("",
"0000000000000000000000000000000000000000000000000000000000000000",
{0, 1}, 0,
"ef3fdfd6c61578fbf5cf35bd3dd33b8009631634d21e42ac33960bd138e50d32"
"111e4caf237ee53ca8ad6426194a88545ddc497a0b466e7d6bbdb0041b2f586b");
TestChaCha20("",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
{0, 0x0706050403020100ULL}, 0,
"f798a189f195e66982105ffb640bb7757f579da31602fc93ec01ac56f85ac3c1"
"34a4547b733b46413042c9440049176905d3be59ea1c53f15916155c2be8241a"
"38008b9a26bc35941e2444177c8ade6689de95264986d95889fb60e84629c9bd"
"9a5acb1cc118be563eb9b3a4a472f82e09a7e778492b562ef7130e88dfe031c7"
"9db9d4f7c7a899151b9a475032b63fc385245fe054e3dd5a97a5f576fe064025"
"d3ce042c566ab2c507b138db853e3d6959660996546cc9c4a6eafdc777c040d7"
"0eaf46f76dad3979e5c5360c3317166a1c894c94a371876a94df7628fe4eaaf2"
"ccb27d5aaae0ad7ad0f9d4b6ad3b54098746d4524d38407a6deb3ab78fab78c9");
}

BOOST_AUTO_TEST_CASE(chacha20_midblock)
Expand Down

0 comments on commit 7f2a985

Please sign in to comment.