Skip to content

Commit 5606d7f

Browse files
committed
Merge bitcoin/bitcoin#28267: crypto: BIP324 ciphersuite follow-up
93cb8f0 refactor: add missing headers for BIP324 ciphersuite (stratospher) d22d5d9 crypto: BIP324 ciphersuite follow-up (stratospher) Pull request description: follow-up to #28008. * move `dummy_tag` variable in FSChaCha20Poly1305 crypto_tests outside of the loop to be reused every time * use easy to read `cipher.last()` in `AEADChaCha20Poly1305::Decrypt()` * comment for initiator in `BIP324Cipher::Initialize()` * systematically damage ciphertext with bit positions in bip324_tests * use 4095 max bytes for `aad` in bip324 fuzz test ACKs for top commit: fanquake: ACK 93cb8f0 - thanks for following up here. Tree-SHA512: 361f3e226d3168fdef69a2eebe6092cfc04ba14ce009420222e762698001eaf8be69a1138dab0be237964509c2b96a41a0b4db5c1df43ef75062f143c5aa741a
2 parents e38c225 + 93cb8f0 commit 5606d7f

File tree

7 files changed

+16
-12
lines changed

7 files changed

+16
-12
lines changed

src/bip324.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88
#include <crypto/chacha20.h>
99
#include <crypto/chacha20poly1305.h>
1010
#include <crypto/hkdf_sha256_32.h>
11+
#include <key.h>
12+
#include <pubkey.h>
1113
#include <random.h>
1214
#include <span.h>
1315
#include <support/cleanse.h>
16+
#include <uint256.h>
1417

1518
#include <algorithm>
1619
#include <assert.h>
1720
#include <cstdint>
1821
#include <cstddef>
22+
#include <iterator>
23+
#include <string>
1924

2025
BIP324Cipher::BIP324Cipher() noexcept
2126
{

src/bip324.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_BIP324_H
66
#define BITCOIN_BIP324_H
77

8+
#include <array>
89
#include <cstddef>
910
#include <optional>
1011

@@ -54,6 +55,7 @@ class BIP324Cipher
5455

5556
/** Initialize when the other side's public key is received. Can only be called once.
5657
*
58+
* initiator is set to true if we are the initiator establishing the v2 P2P connection.
5759
* self_decrypt is only for testing, and swaps encryption/decryption keys, so that encryption
5860
* and decryption can be tested without knowing the other side's private key.
5961
*/

src/crypto/chacha20poly1305.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
#include <support/cleanse.h>
1212

1313
#include <assert.h>
14-
#include <cstdint>
1514
#include <cstddef>
16-
#include <iterator>
1715

1816
AEADChaCha20Poly1305::AEADChaCha20Poly1305(Span<const std::byte> key) noexcept : m_chacha20(UCharCast(key.data()))
1917
{
@@ -95,7 +93,7 @@ bool AEADChaCha20Poly1305::Decrypt(Span<const std::byte> cipher, Span<const std:
9593
m_chacha20.Seek64(nonce, 0);
9694
std::byte expected_tag[EXPANSION];
9795
ComputeTag(m_chacha20, aad, cipher.first(cipher.size() - EXPANSION), expected_tag);
98-
if (timingsafe_bcmp(UCharCast(expected_tag), UCharCast(cipher.data() + cipher.size() - EXPANSION), EXPANSION)) return false;
96+
if (timingsafe_bcmp(UCharCast(expected_tag), UCharCast(cipher.last(EXPANSION).data()), EXPANSION)) return false;
9997

10098
// Decrypt (starting at block 1).
10199
m_chacha20.Crypt(UCharCast(cipher.data()), UCharCast(plain1.data()), plain1.size());

src/crypto/chacha20poly1305.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#define BITCOIN_CRYPTO_CHACHA20POLY1305_H
77

88
#include <cstddef>
9-
#include <cstdlib>
109
#include <stdint.h>
1110

1211
#include <crypto/chacha20.h>

src/test/bip324_tests.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include <chainparams.h>
77
#include <key.h>
88
#include <pubkey.h>
9+
#include <span.h>
910
#include <test/util/random.h>
1011
#include <test/util/setup_common.h>
1112
#include <util/strencodings.h>
1213

1314
#include <array>
14-
#include <vector>
1515
#include <cstddef>
16+
#include <cstdint>
17+
#include <vector>
1618

1719
#include <boost/test/unit_test.hpp>
1820

@@ -131,10 +133,10 @@ void TestBIP324PacketVector(
131133
// Decrypt length
132134
auto to_decrypt = ciphertext;
133135
if (error >= 2 && error <= 9) {
134-
to_decrypt[InsecureRandRange(to_decrypt.size())] ^= std::byte(1U << InsecureRandRange(8));
136+
to_decrypt[InsecureRandRange(to_decrypt.size())] ^= std::byte(1U << (error - 2));
135137
}
136138

137-
// Decrypt length and resize ciphertext to accomodate.
139+
// Decrypt length and resize ciphertext to accommodate.
138140
uint32_t dec_len = dec_cipher.DecryptLength(MakeByteSpan(to_decrypt).first(cipher.LENGTH_LEN));
139141
to_decrypt.resize(dec_len + cipher.EXPANSION);
140142

src/test/crypto_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,11 @@ static void TestFSChaCha20Poly1305(const std::string& plain_hex, const std::stri
300300
for (int it = 0; it < 10; ++it) {
301301
// During it==0 we use the single-plain Encrypt/Decrypt; others use a split at prefix.
302302
size_t prefix = it ? InsecureRandRange(plain.size() + 1) : plain.size();
303+
std::byte dummy_tag[FSChaCha20Poly1305::EXPANSION] = {{}};
303304

304305
// Do msg_idx dummy encryptions to seek to the correct packet.
305306
FSChaCha20Poly1305 enc_aead{key, 224};
306307
for (uint64_t i = 0; i < msg_idx; ++i) {
307-
std::byte dummy_tag[FSChaCha20Poly1305::EXPANSION] = {{}};
308308
enc_aead.Encrypt(Span{dummy_tag}.first(0), Span{dummy_tag}.first(0), dummy_tag);
309309
}
310310

@@ -319,7 +319,6 @@ static void TestFSChaCha20Poly1305(const std::string& plain_hex, const std::stri
319319
// Do msg_idx dummy decryptions to seek to the correct packet.
320320
FSChaCha20Poly1305 dec_aead{key, 224};
321321
for (uint64_t i = 0; i < msg_idx; ++i) {
322-
std::byte dummy_tag[FSChaCha20Poly1305::EXPANSION] = {{}};
323322
dec_aead.Decrypt(dummy_tag, Span{dummy_tag}.first(0), Span{dummy_tag}.first(0));
324323
}
325324

src/test/fuzz/bip324.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <test/util/xoroshiro128plusplus.h>
1212

1313
#include <cstdint>
14-
#include <tuple>
1514
#include <vector>
1615

1716
namespace {
@@ -75,13 +74,13 @@ FUZZ_TARGET(bip324_cipher_roundtrip, .init=Initialize)
7574
// - Bit 0: whether the ignore bit is set in message
7675
// - Bit 1: whether the responder (0) or initiator (1) sends
7776
// - Bit 2: whether this ciphertext will be corrupted (making it the last sent one)
78-
// - Bit 3-4: controls the maximum aad length (max 511 bytes)
77+
// - Bit 3-4: controls the maximum aad length (max 4095 bytes)
7978
// - Bit 5-7: controls the maximum content length (max 16383 bytes, for performance reasons)
8079
unsigned mode = provider.ConsumeIntegral<uint8_t>();
8180
bool ignore = mode & 1;
8281
bool from_init = mode & 2;
8382
bool damage = mode & 4;
84-
unsigned aad_length_bits = 3 * ((mode >> 3) & 3);
83+
unsigned aad_length_bits = 4 * ((mode >> 3) & 3);
8584
unsigned aad_length = provider.ConsumeIntegralInRange<unsigned>(0, (1 << aad_length_bits) - 1);
8685
unsigned length_bits = 2 * ((mode >> 5) & 7);
8786
unsigned length = provider.ConsumeIntegralInRange<unsigned>(0, (1 << length_bits) - 1);

0 commit comments

Comments
 (0)