From 6babf402130a8f3ef3058594750aeaa50b8f5044 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 21 Sep 2022 16:58:13 -0400 Subject: [PATCH] Rename ChaCha20::Seek -> Seek64 to clarify multiple of 64 --- src/bench/chacha20.cpp | 2 +- src/crypto/chacha20.cpp | 2 +- src/crypto/chacha20.h | 4 ++-- src/crypto/chacha_poly_aead.cpp | 8 ++++---- src/test/crypto_tests.cpp | 8 ++++---- src/test/fuzz/crypto_chacha20.cpp | 2 +- src/test/fuzz/crypto_diff_fuzz_chacha20.cpp | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/bench/chacha20.cpp b/src/bench/chacha20.cpp index 656fb833e7a..8d1d1952bc5 100644 --- a/src/bench/chacha20.cpp +++ b/src/bench/chacha20.cpp @@ -16,7 +16,7 @@ static void CHACHA20(benchmark::Bench& bench, size_t buffersize) std::vector key(32,0); ChaCha20 ctx(key.data(), key.size()); ctx.SetIV(0); - ctx.Seek(0); + ctx.Seek64(0); std::vector in(buffersize,0); std::vector out(buffersize,0); bench.batch(in.size()).unit("byte").run([&] { diff --git a/src/crypto/chacha20.cpp b/src/crypto/chacha20.cpp index cdeeee192e4..c72ccccc652 100644 --- a/src/crypto/chacha20.cpp +++ b/src/crypto/chacha20.cpp @@ -68,7 +68,7 @@ void ChaCha20Aligned::SetIV(uint64_t iv) input[15] = iv >> 32; } -void ChaCha20Aligned::Seek(uint64_t pos) +void ChaCha20Aligned::Seek64(uint64_t pos) { input[12] = pos; input[13] = pos >> 32; diff --git a/src/crypto/chacha20.h b/src/crypto/chacha20.h index 12ddef92564..fdef257ed32 100644 --- a/src/crypto/chacha20.h +++ b/src/crypto/chacha20.h @@ -30,7 +30,7 @@ public: void SetIV(uint64_t iv); /** set the 64bit block counter (pos seeks to byte position 64*pos). */ - void Seek(uint64_t pos); + void Seek64(uint64_t pos); /** outputs the keystream of size <64*blocks> into */ void Keystream64(unsigned char* c, size_t blocks); @@ -60,7 +60,7 @@ public: void SetIV(uint64_t iv) { m_aligned.SetIV(iv); } /** set the 64bit block counter (pos seeks to byte position 64*pos). */ - void Seek(uint64_t pos) { m_aligned.Seek(pos); } + void Seek64(uint64_t pos) { m_aligned.Seek64(pos); } /** outputs the keystream of size into */ void Keystream(unsigned char* c, size_t bytes); diff --git a/src/crypto/chacha_poly_aead.cpp b/src/crypto/chacha_poly_aead.cpp index 6511f46adc0..5d135f89879 100644 --- a/src/crypto/chacha_poly_aead.cpp +++ b/src/crypto/chacha_poly_aead.cpp @@ -62,7 +62,7 @@ bool ChaCha20Poly1305AEAD::Crypt(uint64_t seqnr_payload, uint64_t seqnr_aad, int // block counter 0 for the poly1305 key // use lower 32bytes for the poly1305 key // (throws away 32 unused bytes (upper 32) from this ChaCha20 round) - m_chacha_main.Seek(0); + m_chacha_main.Seek64(0); m_chacha_main.Crypt(poly_key, poly_key, sizeof(poly_key)); // if decrypting, verify the tag prior to decryption @@ -85,7 +85,7 @@ bool ChaCha20Poly1305AEAD::Crypt(uint64_t seqnr_payload, uint64_t seqnr_aad, int if (m_cached_aad_seqnr != seqnr_aad) { m_cached_aad_seqnr = seqnr_aad; m_chacha_header.SetIV(seqnr_aad); - m_chacha_header.Seek(0); + m_chacha_header.Seek64(0); m_chacha_header.Keystream(m_aad_keystream_buffer, CHACHA20_ROUND_OUTPUT); } // crypt the AAD (3 bytes message length) with given position in AAD cipher instance keystream @@ -94,7 +94,7 @@ bool ChaCha20Poly1305AEAD::Crypt(uint64_t seqnr_payload, uint64_t seqnr_aad, int dest[2] = src[2] ^ m_aad_keystream_buffer[aad_pos + 2]; // Set the playload ChaCha instance block counter to 1 and crypt the payload - m_chacha_main.Seek(1); + m_chacha_main.Seek64(1); m_chacha_main.Crypt(src + CHACHA20_POLY1305_AEAD_AAD_LEN, dest + CHACHA20_POLY1305_AEAD_AAD_LEN, src_len - CHACHA20_POLY1305_AEAD_AAD_LEN); // If encrypting, calculate and append tag @@ -117,7 +117,7 @@ bool ChaCha20Poly1305AEAD::GetLength(uint32_t* len24_out, uint64_t seqnr_aad, in // we need to calculate the 64 keystream bytes since we reached a new aad sequence number m_cached_aad_seqnr = seqnr_aad; m_chacha_header.SetIV(seqnr_aad); // use LE for the nonce - m_chacha_header.Seek(0); // block counter 0 + m_chacha_header.Seek64(0); // block counter 0 m_chacha_header.Keystream(m_aad_keystream_buffer, CHACHA20_ROUND_OUTPUT); // write keystream to the cache } diff --git a/src/test/crypto_tests.cpp b/src/test/crypto_tests.cpp index d3eef7beb7a..48a46258b0f 100644 --- a/src/test/crypto_tests.cpp +++ b/src/test/crypto_tests.cpp @@ -136,7 +136,7 @@ static void TestChaCha20(const std::string &hex_message, const std::string &hexk std::vector m = ParseHex(hex_message); ChaCha20 rng(key.data(), key.size()); rng.SetIV(nonce); - rng.Seek(seek); + rng.Seek64(seek); std::vector out = ParseHex(hexout); std::vector outres; outres.resize(out.size()); @@ -152,7 +152,7 @@ static void TestChaCha20(const std::string &hex_message, const std::string &hexk if (!hex_message.empty()) { // Manually XOR with the keystream and compare the output rng.SetIV(nonce); - rng.Seek(seek); + rng.Seek64(seek); std::vector only_keystream(outres.size()); rng.Keystream(only_keystream.data(), only_keystream.size()); for (size_t i = 0; i != m.size(); i++) { @@ -631,7 +631,7 @@ static void TestChaCha20Poly1305AEAD(bool must_succeed, unsigned int expected_aa // manually construct the AAD keystream cmp_ctx.SetIV(seqnr_aad); - cmp_ctx.Seek(0); + cmp_ctx.Seek64(0); cmp_ctx.Keystream(cmp_ctx_buffer.data(), 64); BOOST_CHECK(memcmp(expected_aad_keystream.data(), cmp_ctx_buffer.data(), expected_aad_keystream.size()) == 0); // crypt the 3 length bytes and compare the length @@ -659,7 +659,7 @@ static void TestChaCha20Poly1305AEAD(bool must_succeed, unsigned int expected_aa } // set nonce and block counter, output the keystream cmp_ctx.SetIV(seqnr_aad); - cmp_ctx.Seek(0); + cmp_ctx.Seek64(0); cmp_ctx.Keystream(cmp_ctx_buffer.data(), 64); // crypt the 3 length bytes and compare the length diff --git a/src/test/fuzz/crypto_chacha20.cpp b/src/test/fuzz/crypto_chacha20.cpp index 3f552a8cda8..a1090993942 100644 --- a/src/test/fuzz/crypto_chacha20.cpp +++ b/src/test/fuzz/crypto_chacha20.cpp @@ -30,7 +30,7 @@ FUZZ_TARGET(crypto_chacha20) chacha20.SetIV(fuzzed_data_provider.ConsumeIntegral()); }, [&] { - chacha20.Seek(fuzzed_data_provider.ConsumeIntegral()); + chacha20.Seek64(fuzzed_data_provider.ConsumeIntegral()); }, [&] { std::vector output(fuzzed_data_provider.ConsumeIntegralInRange(0, 4096)); diff --git a/src/test/fuzz/crypto_diff_fuzz_chacha20.cpp b/src/test/fuzz/crypto_diff_fuzz_chacha20.cpp index 1b89d557735..0b24b7c3636 100644 --- a/src/test/fuzz/crypto_diff_fuzz_chacha20.cpp +++ b/src/test/fuzz/crypto_diff_fuzz_chacha20.cpp @@ -304,7 +304,7 @@ FUZZ_TARGET(crypto_diff_fuzz_chacha20) }, [&] { uint64_t counter = fuzzed_data_provider.ConsumeIntegral(); - chacha20.Seek(counter); + chacha20.Seek64(counter); ctx.input[12] = counter; ctx.input[13] = counter >> 32; },