Skip to content

Commit 0391176

Browse files
committed
BIP324 Cipher Suite
1 parent b4b59ea commit 0391176

14 files changed

+427
-597
lines changed

src/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,8 @@ crypto_libbitcoin_crypto_base_la_LDFLAGS = $(AM_LDFLAGS) -static
524524
crypto_libbitcoin_crypto_base_la_SOURCES = \
525525
crypto/aes.cpp \
526526
crypto/aes.h \
527-
crypto/chacha_poly_aead.h \
528-
crypto/chacha_poly_aead.cpp \
527+
crypto/bip324_suite.h \
528+
crypto/bip324_suite.cpp \
529529
crypto/chacha20.h \
530530
crypto/chacha20.cpp \
531531
crypto/common.h \

src/Makefile.bench.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ bench_bench_bitcoin_SOURCES = \
1818
bench/bench.cpp \
1919
bench/bench.h \
2020
bench/bench_bitcoin.cpp \
21+
bench/bip324_suite.cpp \
2122
bench/block_assemble.cpp \
2223
bench/ccoins_caching.cpp \
2324
bench/chacha20.cpp \
24-
bench/chacha_poly_aead.cpp \
2525
bench/checkblock.cpp \
2626
bench/checkqueue.cpp \
2727
bench/crypto_hash.cpp \

src/Makefile.test.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ test_fuzz_fuzz_SOURCES = \
254254
test/fuzz/crypto.cpp \
255255
test/fuzz/crypto_aes256.cpp \
256256
test/fuzz/crypto_aes256cbc.cpp \
257+
test/fuzz/crypto_bip324_suite.cpp \
257258
test/fuzz/crypto_chacha20.cpp \
258-
test/fuzz/crypto_chacha20_poly1305_aead.cpp \
259259
test/fuzz/crypto_common.cpp \
260260
test/fuzz/crypto_diff_fuzz_chacha20.cpp \
261261
test/fuzz/crypto_hkdf_hmac_sha256_l32.cpp \

src/bench/bip324_suite.cpp

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright (c) 2019-2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <assert.h>
6+
#include <bench/bench.h>
7+
#include <crypto/bip324_suite.h>
8+
#include <crypto/rfc8439.h>
9+
#include <hash.h>
10+
11+
#include <array>
12+
#include <cstddef>
13+
#include <vector>
14+
15+
/* Number of bytes to process per iteration */
16+
static constexpr uint64_t BUFFER_SIZE_TINY = 64;
17+
static constexpr uint64_t BUFFER_SIZE_SMALL = 256;
18+
static constexpr uint64_t BUFFER_SIZE_LARGE = 1024 * 1024;
19+
20+
static const std::vector<std::byte> zero_vec(BIP324_KEY_LEN, std::byte{0x00});
21+
22+
static void BIP324_CIPHER_SUITE(benchmark::Bench& bench, size_t contents_len, bool include_decryption)
23+
{
24+
BIP324Key zero_arr;
25+
std::memcpy(zero_arr.data(), zero_vec.data(), BIP324_KEY_LEN);
26+
BIP324CipherSuite enc{zero_arr, zero_arr};
27+
BIP324CipherSuite dec{zero_arr, zero_arr};
28+
29+
auto packet_len = BIP324_LENGTH_FIELD_LEN + BIP324_HEADER_LEN + contents_len + RFC8439_EXPANSION;
30+
31+
std::vector<std::byte> in(contents_len, std::byte{0x00});
32+
std::vector<std::byte> out(packet_len, std::byte{0x00});
33+
34+
BIP324HeaderFlags flags{BIP324_NONE};
35+
36+
bench.batch(contents_len).unit("byte").run([&] {
37+
// encrypt or decrypt the buffer with a static key
38+
const bool crypt_ok_1 = enc.Crypt(in, out, flags, true);
39+
assert(crypt_ok_1);
40+
41+
if (include_decryption) {
42+
// if we decrypt, we need to decrypt the length first
43+
std::array<std::byte, BIP324_LENGTH_FIELD_LEN> encrypted_pkt_len;
44+
std::memcpy(encrypted_pkt_len.data(), out.data(), BIP324_LENGTH_FIELD_LEN);
45+
(void)dec.DecryptLength(encrypted_pkt_len);
46+
const bool crypt_ok_2 = dec.Crypt({out.data() + BIP324_LENGTH_FIELD_LEN, out.size() - BIP324_LENGTH_FIELD_LEN}, in, flags, false);
47+
assert(crypt_ok_2);
48+
}
49+
});
50+
}
51+
52+
static void BIP324_CIPHER_SUITE_64BYTES_ONLY_ENCRYPT(benchmark::Bench& bench)
53+
{
54+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_TINY, false);
55+
}
56+
57+
static void BIP324_CIPHER_SUITE_256BYTES_ONLY_ENCRYPT(benchmark::Bench& bench)
58+
{
59+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_SMALL, false);
60+
}
61+
62+
static void BIP324_CIPHER_SUITE_1MB_ONLY_ENCRYPT(benchmark::Bench& bench)
63+
{
64+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_LARGE, false);
65+
}
66+
67+
static void BIP324_CIPHER_SUITE_64BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench)
68+
{
69+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_TINY, true);
70+
}
71+
72+
static void BIP324_CIPHER_SUITE_256BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench)
73+
{
74+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_SMALL, true);
75+
}
76+
77+
static void BIP324_CIPHER_SUITE_1MB_ENCRYPT_DECRYPT(benchmark::Bench& bench)
78+
{
79+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_LARGE, true);
80+
}
81+
82+
// Add Hash() (dbl-sha256) bench for comparison
83+
84+
static void HASH(benchmark::Bench& bench, size_t buffersize)
85+
{
86+
uint8_t hash[CHash256::OUTPUT_SIZE];
87+
std::vector<uint8_t> in(buffersize, 0);
88+
bench.batch(in.size()).unit("byte").run([&] {
89+
CHash256().Write(in).Finalize(hash);
90+
});
91+
}
92+
93+
static void HASH_64BYTES(benchmark::Bench& bench)
94+
{
95+
HASH(bench, BUFFER_SIZE_TINY);
96+
}
97+
98+
static void HASH_256BYTES(benchmark::Bench& bench)
99+
{
100+
HASH(bench, BUFFER_SIZE_SMALL);
101+
}
102+
103+
static void HASH_1MB(benchmark::Bench& bench)
104+
{
105+
HASH(bench, BUFFER_SIZE_LARGE);
106+
}
107+
108+
BENCHMARK(BIP324_CIPHER_SUITE_64BYTES_ONLY_ENCRYPT, benchmark::PriorityLevel::HIGH);
109+
BENCHMARK(BIP324_CIPHER_SUITE_256BYTES_ONLY_ENCRYPT, benchmark::PriorityLevel::HIGH);
110+
BENCHMARK(BIP324_CIPHER_SUITE_1MB_ONLY_ENCRYPT, benchmark::PriorityLevel::HIGH);
111+
BENCHMARK(BIP324_CIPHER_SUITE_64BYTES_ENCRYPT_DECRYPT, benchmark::PriorityLevel::HIGH);
112+
BENCHMARK(BIP324_CIPHER_SUITE_256BYTES_ENCRYPT_DECRYPT, benchmark::PriorityLevel::HIGH);
113+
BENCHMARK(BIP324_CIPHER_SUITE_1MB_ENCRYPT_DECRYPT, benchmark::PriorityLevel::HIGH);
114+
BENCHMARK(HASH_64BYTES, benchmark::PriorityLevel::HIGH);
115+
BENCHMARK(HASH_256BYTES, benchmark::PriorityLevel::HIGH);
116+
BENCHMARK(HASH_1MB, benchmark::PriorityLevel::HIGH);

src/bench/chacha_poly_aead.cpp

-126
This file was deleted.

src/crypto/bip324_suite.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) 2019-2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <crypto/bip324_suite.h>
6+
7+
#include <crypto/common.h>
8+
#include <crypto/poly1305.h>
9+
#include <crypto/sha256.h>
10+
#include <support/cleanse.h>
11+
12+
#include <assert.h>
13+
#include <cstring>
14+
#include <string.h>
15+
16+
BIP324CipherSuite::~BIP324CipherSuite()
17+
{
18+
memory_cleanse(key_P.data(), key_P.size());
19+
}
20+
21+
void BIP324CipherSuite::Rekey()
22+
{
23+
ChaCha20 rekey_c20(UCharCast(key_P.data()));
24+
std::array<std::byte, NONCE_LENGTH> rekey_nonce;
25+
std::memset(rekey_nonce.data(), 0xFF, 4);
26+
std::memcpy(rekey_nonce.data() + 4, nonce.data() + 4, NONCE_LENGTH - 4);
27+
rekey_c20.SetRFC8439Nonce(rekey_nonce);
28+
rekey_c20.SeekRFC8439(1);
29+
rekey_c20.Keystream(reinterpret_cast<unsigned char*>(key_P.data()), BIP324_KEY_LEN);
30+
}
31+
32+
bool BIP324CipherSuite::Crypt(const Span<const std::byte> input, Span<std::byte> output,
33+
BIP324HeaderFlags& flags, bool encrypt)
34+
{
35+
// check buffer boundaries
36+
if (
37+
// if we encrypt, make sure the destination has the space for the encrypted length field, header, contents and MAC
38+
(encrypt && (output.size() < BIP324_LENGTH_FIELD_LEN + BIP324_HEADER_LEN + input.size() + RFC8439_EXPANSION)) ||
39+
// if we decrypt, make sure the source contains at least the encrypted header + mac and the destination has the space for the input - MAC - header
40+
(!encrypt && (input.size() < BIP324_HEADER_LEN + RFC8439_EXPANSION || output.size() < input.size() - BIP324_HEADER_LEN - RFC8439_EXPANSION))) {
41+
return false;
42+
}
43+
44+
if (encrypt) {
45+
// input is just the contents
46+
// output will be encrypted contents length + encrypted (header and contents) + mac tag
47+
uint32_t contents_len = input.size();
48+
WriteLE32(reinterpret_cast<unsigned char*>(&contents_len), contents_len);
49+
50+
std::vector<std::byte> header_and_contents(BIP324_HEADER_LEN + input.size());
51+
52+
std::memcpy(header_and_contents.data(), &flags, BIP324_HEADER_LEN);
53+
if (!input.empty()) {
54+
std::memcpy(header_and_contents.data() + BIP324_HEADER_LEN, input.data(), input.size());
55+
}
56+
57+
auto write_pos = output.data();
58+
fsc20.Crypt({reinterpret_cast<std::byte*>(&contents_len), BIP324_LENGTH_FIELD_LEN},
59+
{write_pos, BIP324_LENGTH_FIELD_LEN});
60+
write_pos += BIP324_LENGTH_FIELD_LEN;
61+
RFC8439Encrypt({}, key_P, nonce, header_and_contents, {write_pos, BIP324_HEADER_LEN + input.size() + RFC8439_EXPANSION});
62+
} else {
63+
// we must use BIP324CipherSuite::DecryptLength before calling BIP324CipherSuite::Crypt
64+
// input is encrypted (header + contents) and the MAC tag i.e. the RFC8439 ciphertext blob
65+
// decrypted header will be put in flags and output will be plaintext contents.
66+
std::vector<std::byte> decrypted_header_and_contents(input.size() - RFC8439_EXPANSION);
67+
auto authenticated = RFC8439Decrypt({}, key_P, nonce, input, decrypted_header_and_contents);
68+
if (!authenticated) {
69+
return false;
70+
}
71+
72+
std::memcpy(&flags, decrypted_header_and_contents.data(), BIP324_HEADER_LEN);
73+
if (!output.empty()) {
74+
std::memcpy(output.data(),
75+
decrypted_header_and_contents.data() + BIP324_HEADER_LEN,
76+
input.size() - BIP324_HEADER_LEN - RFC8439_EXPANSION);
77+
}
78+
}
79+
80+
packet_counter++;
81+
if (packet_counter % REKEY_INTERVAL == 0) {
82+
Rekey();
83+
}
84+
set_nonce();
85+
return true;
86+
}
87+
88+
uint32_t BIP324CipherSuite::DecryptLength(const std::array<std::byte, BIP324_LENGTH_FIELD_LEN>& encrypted_length)
89+
{
90+
std::array<uint8_t, BIP324_LENGTH_FIELD_LEN> length_buffer;
91+
fsc20.Crypt(encrypted_length, MakeWritableByteSpan(length_buffer));
92+
93+
return (uint32_t{length_buffer[0]}) |
94+
(uint32_t{length_buffer[1]} << 8) |
95+
(uint32_t{length_buffer[2]} << 16);
96+
}

0 commit comments

Comments
 (0)