Skip to content

Commit ee6112e

Browse files
committed
BIP324 Cipher Suite
1 parent 3b7abc2 commit ee6112e

13 files changed

+463
-591
lines changed

src/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,8 @@ crypto_libbitcoin_crypto_base_la_LDFLAGS = $(AM_LDFLAGS) -static
514514
crypto_libbitcoin_crypto_base_la_SOURCES = \
515515
crypto/aes.cpp \
516516
crypto/aes.h \
517-
crypto/chacha_poly_aead.h \
518-
crypto/chacha_poly_aead.cpp \
517+
crypto/bip324_suite.h \
518+
crypto/bip324_suite.cpp \
519519
crypto/chacha20.h \
520520
crypto/chacha20.cpp \
521521
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
@@ -255,8 +255,8 @@ test_fuzz_fuzz_SOURCES = \
255255
test/fuzz/crypto.cpp \
256256
test/fuzz/crypto_aes256.cpp \
257257
test/fuzz/crypto_aes256cbc.cpp \
258+
test/fuzz/crypto_bip324_suite.cpp \
258259
test/fuzz/crypto_chacha20.cpp \
259-
test/fuzz/crypto_chacha20_poly1305_aead.cpp \
260260
test/fuzz/crypto_common.cpp \
261261
test/fuzz/crypto_diff_fuzz_chacha20.cpp \
262262
test/fuzz/crypto_hkdf_hmac_sha256_l32.cpp \

src/bench/bip324_suite.cpp

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

src/bench/chacha_poly_aead.cpp

-126
This file was deleted.

src/crypto/bip324_suite.cpp

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
#ifndef HAVE_TIMINGSAFE_BCMP
17+
18+
int timingsafe_bcmp(const unsigned char* b1, const unsigned char* b2, size_t n)
19+
{
20+
const unsigned char *p1 = b1, *p2 = b2;
21+
int ret = 0;
22+
23+
for (; n > 0; n--)
24+
ret |= *p1++ ^ *p2++;
25+
return (ret != 0);
26+
}
27+
28+
#endif // TIMINGSAFE_BCMP
29+
30+
BIP324CipherSuite::~BIP324CipherSuite()
31+
{
32+
memory_cleanse(key_P.data(), key_P.size());
33+
}
34+
35+
void BIP324CipherSuite::CommitToKeys(const Span<const std::byte> data, bool commit_to_L, bool commit_to_P)
36+
{
37+
if (commit_to_L) {
38+
fsc20.CommitToKey(data);
39+
}
40+
41+
if (commit_to_P) {
42+
assert(CSHA256::OUTPUT_SIZE == BIP324_KEY_LEN);
43+
auto hasher = rekey_hasher;
44+
hasher << MakeUCharSpan(data) << MakeUCharSpan(key_P);
45+
auto new_key = hasher.GetSHA256();
46+
memcpy(key_P.data(), new_key.data(), BIP324_KEY_LEN);
47+
}
48+
49+
set_nonce();
50+
}
51+
52+
bool BIP324CipherSuite::Crypt(const Span<const std::byte> input, Span<std::byte> output,
53+
BIP324HeaderFlags& flags, bool encrypt)
54+
{
55+
// check buffer boundaries
56+
if (
57+
// if we encrypt, make sure the destination has the space for the encrypted length field, header, contents and MAC
58+
(encrypt && (output.size() < BIP324_LENGTH_FIELD_LEN + BIP324_HEADER_LEN + input.size() + RFC8439_EXPANSION)) ||
59+
// 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
60+
(!encrypt && (input.size() < BIP324_HEADER_LEN + RFC8439_EXPANSION || output.size() < input.size() - BIP324_HEADER_LEN - RFC8439_EXPANSION))) {
61+
return false;
62+
}
63+
64+
if (encrypt) {
65+
// input is just the contents
66+
// output will be encrypted contents length + encrypted (header and contents) + mac tag
67+
uint32_t contents_len = input.size();
68+
WriteLE32(reinterpret_cast<unsigned char*>(&contents_len), contents_len);
69+
70+
std::vector<std::byte> header_and_contents(BIP324_HEADER_LEN + input.size());
71+
72+
memcpy(header_and_contents.data(), &flags, BIP324_HEADER_LEN);
73+
if (!input.empty()) {
74+
memcpy(header_and_contents.data() + BIP324_HEADER_LEN, input.data(), input.size());
75+
}
76+
77+
auto write_pos = output.data();
78+
fsc20.Crypt({reinterpret_cast<std::byte*>(&contents_len), BIP324_LENGTH_FIELD_LEN},
79+
{write_pos, BIP324_LENGTH_FIELD_LEN});
80+
write_pos += BIP324_LENGTH_FIELD_LEN;
81+
RFC8439Encrypt({}, key_P, nonce, header_and_contents, {write_pos, BIP324_HEADER_LEN + input.size() + RFC8439_EXPANSION});
82+
} else {
83+
// we must use BIP324CipherSuite::DecryptLength before calling BIP324CipherSuite::Crypt
84+
// input is encrypted (header + contents) and the MAC tag i.e. the RFC8439 ciphertext blob
85+
// decrypted header will be put in flags and output will be plaintext contents.
86+
std::vector<std::byte> decrypted_header_and_contents(input.size() - RFC8439_EXPANSION);
87+
auto authenticated = RFC8439Decrypt({}, key_P, nonce, input, decrypted_header_and_contents);
88+
if (!authenticated) {
89+
return false;
90+
}
91+
92+
memcpy(&flags, decrypted_header_and_contents.data(), BIP324_HEADER_LEN);
93+
if (!output.empty()) {
94+
memcpy(output.data(),
95+
decrypted_header_and_contents.data() + BIP324_HEADER_LEN,
96+
input.size() - BIP324_HEADER_LEN - RFC8439_EXPANSION);
97+
}
98+
}
99+
100+
packet_counter++;
101+
if (packet_counter % REKEY_INTERVAL == 0) {
102+
// Rekey key_P. key_L is automatically re-keyed since we're using a forward-secure version
103+
// of ChaCha20, FSChacha20
104+
CommitToKeys({(std::byte*)nullptr, 0}, false, true);
105+
}
106+
set_nonce();
107+
return true;
108+
}
109+
110+
uint32_t BIP324CipherSuite::DecryptLength(const std::array<std::byte, BIP324_LENGTH_FIELD_LEN>& encrypted_length)
111+
{
112+
std::array<uint8_t, BIP324_LENGTH_FIELD_LEN> length_buffer;
113+
fsc20.Crypt(encrypted_length, MakeWritableByteSpan(length_buffer));
114+
115+
return (uint32_t{length_buffer[0]}) |
116+
(uint32_t{length_buffer[1]} << 8) |
117+
(uint32_t{length_buffer[2]} << 16);
118+
}

0 commit comments

Comments
 (0)